Popwindow的使用
来源:互联网 发布:乐天软件 编辑:程序博客网 时间:2024/06/07 01:11
1.在maniAcitity里面写
package com.bawei.day11_popwindow;import android.os.Bundle;import android.app.Activity;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.view.Gravity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.PopupWindow;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private Button btnFirst, btnThrid, btnFour;private PopupWindow pw;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 初始化控件initView();// 初始化popWindowinitPopWindow();}private void initView() {btnFirst = (Button) findViewById(R.id.btn_first);btnThrid = (Button) findViewById(R.id.btn_third);btnFour = (Button) findViewById(R.id.btnFour);btnFirst.setOnClickListener(this);btnThrid.setOnClickListener(this);btnFour.setOnClickListener(this);}private void initPopWindow() {// 1.把popWindow填充到当前界面View view = View.inflate(MainActivity.this, R.layout.my_popwindow, null);Button btnToast = (Button) view.findViewById(R.id.btn_toast);Button btnCloase = (Button) view.findViewById(R.id.btn_close);btnToast.setOnClickListener(this);btnCloase.setOnClickListener(this);// 2.创建一个popWindowpw = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);// 3.设置外部可点击 *** 必须给popWindow设置一个背景pw.setOutsideTouchable(true);pw.setBackgroundDrawable(new ColorDrawable(Color.RED));}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_first:// 参数 显示位置,相对的控件pw.showAsDropDown(btnFirst);break;case R.id.btn_third:// 参数: 1.相对的控件 2.偏移量 x轴方向 3.偏移量 y轴方向pw.showAsDropDown(btnThrid, 100, 100);break;case R.id.btnFour:// 参数 1:父容器 2.父容器的位置 3.xoff X轴偏移量 4.yoff Y轴偏移量pw.showAtLocation(btnFour, Gravity.CENTER, 100, 100);break;case R.id.btnClose:// 关闭popWindowpw.dismiss();break;case R.id.btn_toast:// popWindow弹吐司的按钮,弹一个吐司出来Toast.makeText(MainActivity.this, "这是一个popWindow",Toast.LENGTH_SHORT).show();break;case R.id.btn_close:// 关闭popWindowpw.dismiss();default:break;}}}
2.在main_activity.xml写
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/btn_first" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一个参数,弹出popWindow" /> <Button android:id="@+id/btnFour" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置位置,弹出popWindow" /> <Button android:id="@+id/btn_third" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="三个参数,弹出popWindow" /> <Button android:id="@+id/btnClose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭" /></LinearLayout>3.自定义的my_pop.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" > <Button android:id="@+id/btn_toast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="弹吐司" /> <Button android:id="@+id/btn_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点我关闭" /></LinearLayout>
0 0
- popwindow的基本使用
- android popwindow的使用
- android popwindow的使用
- popWindow的使用
- popwindow的使用
- PopWindow的基本使用
- popwindow的基本使用
- PopWindow的使用
- Popwindow的使用
- Popwindow的使用
- popwindow的使用总结
- Android中 Popwindow的使用
- android中popwindow的使用
- Popwindow的介绍和使用
- POPWindow的使用技巧盲点
- AN popwindow使用的一个简单例子
- popwindow的使用(全屏变暗)
- Popwindow弹出框的模板使用
- LeetCode OJ(11)
- javascript 对象的三个属性 二十六
- VC++6.0的组建菜单选项功能解释
- C语言 字符数组与字符指针比较
- 区块链开发的环境搭建
- Popwindow的使用
- 推荐的c/c++开源库
- 《现代操作系统》读书笔记——第二章 进程与线程
- Android中的Java与JavaScript方法互调
- 构造函数中,成员变量一定要通过初始化列表来初始化的几种情况
- echarts框架创建各种图表
- Linux - 常用命令(长期更新)
- 链表的简单介绍
- HihoCoder 1249(2015 Asia Beijing Regional Contest)