PopupWindow的初步使用
来源:互联网 发布:电信 80端口不能映射 编辑:程序博客网 时间:2024/05/16 08:53
在开发中会用到很多关于对话框的样式 , 但是 对话框是属于比较重量级的 , 而PopupWindow相对来说比对话框小很多,就是一个小的悬浮窗体显示在界面中,并且还可以任意的 移位
首先 : 先定义主页中的 布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/root" tools:context="com.tdcq.popupwindowdemo.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="主界面" android:textSize="30sp" android:id="@+id/textView"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="指定位置打开popupWindow窗口" android:id="@+id/button" android:layout_below="@+id/textView" android:layout_centerHorizontal="true"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下拉方式打开popupWindow窗口" android:id="@+id/button3" android:layout_below="@+id/button" android:layout_centerHorizontal="true"/></RelativeLayout>
然后定义 PopupWindow 中需要的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="popupwindow 界面"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭按钮" android:id="@+id/button4" android:layout_below="@+id/textView2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="32dp"/></LinearLayout>
主页中的代码
package com.tdcq.popupwindowdemo;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.Gravity;import android.view.View;import android.widget.Button;import android.widget.PopupWindow;public class MainActivity extends AppCompatActivity { private Button button; private Button button3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View view = this.getLayoutInflater().inflate(R.layout.popupwindow, null); /** * 1.参数是 PopupWindow 显示的内容, 这里将上面的布局作为显示内容 * 2.参数是窗体的宽度 * 3.参数是窗体的高度 */ final PopupWindow popupWindow = new PopupWindow(view, 500, 700); // 在某个位置上显示popupWindow 到手机屏幕上 button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /* * * 1.参数是要显示在哪个负窗体中, 而实际上就是显示在当前的 界面中 所以直接在activity_main 定义一个id即可 * 2.参数是对齐方式 (一般指定为 中心对齐 或者 左边定点对齐) * 3.参数是 水平方向的偏移量 * 4.参数是竖直方向的偏移量 * * 从内部变量popupwindow访问内部类 , 需要将其定义为final类型的 * */ popupWindow.showAtLocation(findViewById(R.id.button), Gravity.CENTER, 20, 20); } }); //下拉的方式显示popupWindow 到手机屏幕上 button3 = (Button) findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.showAsDropDown(v); } }); //获取 popupWindow 中的 关闭按钮 view.findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //关闭popupWindow popupWindow.dismiss(); } }); }}
注意 在 点击事件里面定义 PopupWindow 时 会报一个错误提示
从内部变量popupwindow访问内部类 , 需要将其定义为final类型的
代码: http://download.csdn.net/detail/q9104422999/9627557
0 0
- PopupWindow的初步使用
- Android PopupWindow初步使用
- Android PopupWindow的使用
- Android PopupWindow的使用
- Android PopupWindow的使用!!!
- 关于popupWindow的使用
- PopupWindow的使用
- PopupWindow的使用例子
- PopupWindow的使用
- popupWindow的使用心得
- PopupWindow的使用
- PopupWindow的使用
- Android PopupWindow的使用!!!
- PopupWindow的使用
- PopupWindow的使用
- popupWindow的使用
- PopupWindow的使用
- PopupWindow的使用
- MapReduce算法形式七:多个mapreduce任务
- Android Support Design库之SnackBar初使用
- make menuconfig过程讲解
- Android系统相册换头像——最短的代码
- TCP-IP详解:SACK选项(Selective Acknowledgment)
- PopupWindow的初步使用
- 【备份专题】备份软件体系架构解析
- Android编译系统分析五:system.img的生成过程
- 如何保持命令行窗口文本复制到WORD格式不变?
- service php-fpm restart
- jsp-request中文乱码
- HDU 5873 Football Games 【】
- 华为OJ——输入整型数组和排序标识,对其元素按照升序或降序进行排序
- 认识jQuery 在笔记手打 可能有错