UI组件:PopupWindows的详细使用(一)
来源:互联网 发布:师洋淘宝骂人 编辑:程序博客网 时间:2024/06/06 18:03
一、定义
在官方文档里面,对PopupWindow做出了以下定义:
A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.
意思就是这个popup window可以用于展示任意一个view,而且这个window会浮动于当前的activity之上。
二、构造器
PopupWindow(View contentView)Create a new non focusable popup window which can display the contentView.
PopupWindow(int width, int height)Create a new empty, non focusable popup window.
PopupWindow(View contentView, int width, int height)Create a new non focusable popup window which can display the contentView.
PopupWindow(View contentView, int width, int height, boolean focusable)Create a new popup window which can display the contentView.由上述第三个构造器可知,在构造一个popup window的时候,需要传递一个view、该popup window的高度、宽度,这也是一个标准的popup window所需要的,但构造器一和二缺少了某些方法,这个可以通过它的方法传递相应的view或者高度、宽度。而构造器四,则多了一个focusable参数,代表是否获得焦点。
三、主要方法
1、Set方法:
//为该popup window的显示设置一个动画效果 setAnimationStyle(int animationStyle) //为该popup window设置一个背景 setBackgroundDrawable(Drawable background) //设置popup window的内容 setContentView(View contentView) //设置popup window的宽和高 setWidth(int width)/setHeight(int height) //设置该popup window是否获得焦点 setFocusable(boolean focusable) //设置popup window 之外的部分是否能点击 setOutsideTouchable(boolean touchable)
2、显示函数:
//在anchor的左下方显示 showAsDropDown(View anchor) //在anchor的左下方显示,x轴上偏移xoff,y轴上偏移yoff showAsDropDown(View anchor, int xoff, int yoff) //相对于父控件的显示位置,x、y为偏移量,gravity可选为Gravity.CENTER正中央,Gravity.BOTTOM下方 showAtLocation(View parent, int gravity, int x, int y)
四、简单的Popup Window的显示:
通过以下代码,将实现下图的PopupWindow的弹出:
(一)MainActivity的布局:布局很简单,只有一个按钮,以及改了一下背景颜色,这里不再给出
(二)PopupWindow的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff"> <View android:layout_width="match_parent" android:layout_height="2.25dp" android:background="#fa7829"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/pop_football" android:text="足球" android:gravity="center" android:textSize="20sp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#160f36"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/pop_tennis" android:text="网球" android:gravity="center" android:textSize="20sp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#160f36"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/pop_basketball" android:text="篮球" android:gravity="center" android:textSize="20sp"/> <View android:layout_width="match_parent" android:layout_height="1dp"/></LinearLayout>(三)MainActivity.java:
public class MainActivity extends Activity implements View.OnClickListener { private Button button; PopupWindow mPopupWindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button= (Button) findViewById(R.id.btn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopupWindowShow(); } }); } private void PopupWindowShow() { View contentView= LayoutInflater.from(MainActivity.this).inflate(R.layout.contentview,null); mPopupWindow=new PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,true); TextView tv1= (TextView) contentView.findViewById(R.id.pop_football); TextView tv2= (TextView) contentView.findViewById(R.id.pop_tennis); TextView tv3= (TextView) contentView.findViewById(R.id.pop_basketball); tv1.setOnClickListener(this); tv2.setOnClickListener(this); tv3.setOnClickListener(this); View rootView=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null); mPopupWindow.showAtLocation(rootView, Gravity.BOTTOM,0,0); } @Override public void onClick(View v) { int id=v.getId(); switch (id){ case R.id.pop_football: Toast.makeText(this,"clicked football",Toast.LENGTH_SHORT).show(); mPopupWindow.dismiss(); break; case R.id.pop_tennis: Toast.makeText(this,"clicked tennis",Toast.LENGTH_SHORT).show(); mPopupWindow.dismiss(); break; case R.id.pop_basketball: Toast.makeText(this,"clicked basketball",Toast.LENGTH_SHORT).show(); mPopupWindow.dismiss(); break; } }}
对PopupWindowShow()函数做出如下解释:
①以下两行代码,首先获得popup window的布局文件,通过LayoutInflater获得,而LayoutInflater通过当前的context获得,
然后通过PopupWindow的构造器,传递contentView,以及相应的宽高,这里宽高选择wrap_content,最后一个true表示获得焦点。
View contentView= LayoutInflater.from(MainActivity.this).inflate(R.layout.contentview,null);mPopupWindow=new PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,true);②首先,通过contentView获取在它内部的三个TextView,然后设置监听器。注意:如果没有写成contentView.findViewById,而直接使用findViewById,则会出错,因为这三个TextView是依附于contentView的,而不是父布局。
TextView tv1= (TextView) contentView.findViewById(R.id.pop_football);TextView tv2= (TextView) contentView.findViewById(R.id.pop_tennis);TextView tv3= (TextView) contentView.findViewById(R.id.pop_basketball);tv1.setOnClickListener(this);tv2.setOnClickListener(this);tv3.setOnClickListener(this);③先获得父布局,即MainActivity的布局,然后利用showAtLocation显示在父布局的底部。
View rootView=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);mPopupWindow.showAtLocation(rootView, Gravity.BOTTOM,0,0);
0 0
- UI组件:PopupWindows的详细使用(一)
- 安卓中PopupWindows的使用
- UI组件之 ProgressBar及其子类(一)ProgressBar进度条的使用
- 【Kendo UI 中文教程】使用Grid组件完成基本的CRUD操作(一)
- Unity UI(一):UI组件概览
- 【UI】UILabel的详细使用
- Android的UI组件之ListView(一)
- 常用的自定义UI组件- 一:TimeView
- popupWindows的位置设置
- 读书笔记(一)-- Flume组件的详细介绍
- popupwindows
- popupwindows
- android-UI组件(一):布局管理器
- Android UI总结(一) AutoCompleteTextView 组件及一些较为常见的组件
- 【ife】任务四十:UI组件之日历组件(一)
- JSF笔记 – JSF中的三大核心组件 UI标签的详细介绍和使用举例
- 自定义使用Adapter的组件(一)
- UI组件之AdapterView及其子类(六)ExpandableListView组件和ExpandableListActivity的使用
- CA dis4 notes
- 如何在静音模式下播放声音(需要设置AVAudioSession)
- Markdown使用技巧总结
- ZeroC Ice IceGrid Node和IceGrid
- 为什么 Java 只有值传递,但 C# 既有值传递,又有引用传递,这种语言设计有哪些好处?
- UI组件:PopupWindows的详细使用(一)
- Andrew Ng机器学习笔记之局部加权回归
- 使用 ContentProviderOperation 来提升性能
- 准确率 召回率 调和平均数等
- mount -o nolock
- link和import的区别
- Java的Font类
- phpcms安装摘要
- (4.5.3.4)finished with non-zero exit value 2