Android studio实现PopupWindow弹出框效果
作者:Be your bubble 时间:2021-12-19 11:12:50
本文实例为大家分享了Android studio实现PopupWindow弹出框的具体代码,供大家参考,具体内容如下
实现步骤:
第一步:自定义.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="提示"
android:background="@drawable/click"
android:textSize="28sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:text="你真的要离开吗"
android:textColor="#ff0400"
android:background="@drawable/background"
android:textSize="28sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50sp"
android:orientation="horizontal"
android:background="@drawable/bb"
android:gravity="center">
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="残忍离开"
android:textSize="24sp"/>
</LinearLayout>
</LinearLayout>
第二步:获取layoutInflater对象
第三步:调用inflater()方法获取View对象
第四步:创建PopupWindow对象
第五步:调用PopupWindow的showAsDropDown或者showAsLocation方法显示对话框窗口
下拉式
默认xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="对话框"
android:onClick="onclick"/>
</LinearLayout>
java:
package com.example.catalogin;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main2);
}
PopupWindow popupWindow;
public void onclick(View v1){
LayoutInflater inflater=LayoutInflater.from( this );
View myview=inflater.inflate(R.layout.catalogin,null);//引用自定义布局
popupWindow=new PopupWindow( myview,600,500 );//后面是像素大小
myview.findViewById(R.id.but).setOnClickListener( new View.OnClickListener() {//获取布局里面按钮
@Override
public void onClick(View v) {
popupWindow.dismiss();//点击按钮对话框消失
Toast.makeText( Main2Activity.this, "点击了残忍离开", Toast.LENGTH_SHORT ).show();
}
} );
popupWindow.showAsDropDown(v1);
}
}
弹出式
代码popupWindow.showAsDropDown(v1);
换成popupWindow.showAtLocation( v1,Gravity.CENTER,0,0 );//偏移量
来源:https://blog.csdn.net/qq_41218412/article/details/89285280
标签:Android,studio,弹出框
0
投稿
猜你喜欢
JavaWeb Hibernate使用全面介绍
2021-09-13 11:26:07
java文件重命名(文件批量重命名)实例程序代码分享
2023-07-20 06:45:06
全面理解java中的异常处理机制
2023-10-26 04:08:20
Java 异步编程实践_动力节点Java学院整理
2023-06-18 17:04:12
WPF实现文字粒子闪烁动画效果
2022-01-12 23:56:45
Spring整合Mybatis思路梳理总结
2021-08-19 18:46:38
从汇编码分析java对象的创建过程(推荐)
2023-06-09 18:05:17
Java抽象类的概念讲解
2023-11-04 13:40:35
C# 泛型集合类List<T>使用总结
2021-08-30 02:04:01
Android组件ViewStub基本使用方法详解
2022-01-15 07:30:54
SpringBoot如何集成PageHelper分页功能
2022-10-22 03:18:34
SpringBoot整合WebService服务的实现代码
2021-11-11 14:59:11
SpringBoot Actuator潜在的OOM问题的解决
2021-08-26 06:36:43
C#预处理器指令的用法实例分析
2023-03-09 16:21:07
浅谈Java中Spring Boot的优势
2022-12-25 17:36:52
idea无法切换分支报错问题及解决
2023-12-14 22:39:26
java 类加载与自定义类加载器详解
2022-05-21 04:31:31
Android 两个Fragment之间传递数据实例详解
2022-06-23 19:30:09
C#委托初级使用的实例代码
2022-12-01 09:59:44
详解Java中方法重写与重载的区别(面试高频问点)
2022-07-19 10:36:48