弹出小窗口,PopupWindow的使用

在程序里弹出一个小窗口,像系统的MediaController一样,具体做法:先在mail.xml的layout布局里加一个id,这个到后面会用到的,

1 <?xml version="1.0" encoding="utf-8"?>
2  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 android:id="@+id/linear"<!--这个一定要加-->
7 >

接着布局文件里写一个control.xml文件,里面放一些要显示的widget,

1 <?xml version="1.0" encoding="utf-8"?>
2  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 android:gravity="center"
7 android:background="#00000000"
8 >
9 <LinearLayout
10 android:orientation="horizontal"
11 android:layout_width="fill_parent"
12 android:layout_height="wrap_content"
13 android:gravity="center_horizontal"
14 android:layout_marginTop="5dip"
15 android:layout_marginBottom="5dip"
16 >
17 <ImageButton
18 android:id="@+id/sounddown"
19 android:layout_width="wrap_content"
20 android:layout_height="wrap_content"
21 android:src="@drawable/sounddown"
22 />
23
24 <TextView
25 android:id="@+id/textNow"
26 android:layout_width="wrap_content"
27 android:layout_height="wrap_content"
28 android:textSize="20sp"
29 android:text=" "
30 />
31 <SeekBar
32 android:id="@+id/seekbar"
33 android:layout_width="500dip"
34 android:layout_height="wrap_content"
35 android:max="10000"
36 />
37 <TextView
38 android:id="@+id/textAll"
39 android:layout_width="wrap_content"
40 android:layout_height="wrap_content"
41 android:textSize="20sp"
42 android:text=" "
43 />
44 <ImageButton
45 android:id="@+id/soundup"
46 android:layout_width="wrap_content"
47 android:layout_height="wrap_content"
48 android:src="@drawable/soundup"
49 />
50 </LinearLayout>
51 <LinearLayout
52 android:orientation="horizontal"
53 android:layout_width="fill_parent"
54 android:layout_height="wrap_content"
55 android:gravity="center_horizontal"
56 android:background="#0000003D"
57 >
58
59 </LinearLayout>
60
61 </LinearLayout>

最后就是java文件,

1 public class MainActivity extends Activity {
2 /** Called when the activity is first created. */
3 private Button show;
4 public PopupWindow mPopupWindow;
5 private boolean boo=true;
6 @Override
7 public void onCreate(Bundle savedInstanceState) {
8 super.onCreate(savedInstanceState);
9 setContentView(R.layout.main);
10 show = (Button) findViewById(R.id.button);
11 show.setOnClickListener(new Button.OnClickListener(){
12
13 @Override
14 public void onClick(View v) {
15 // TODO Auto-generated method stub
16   if(boo){
17 boo=false;
18 check();
19 mPopupWindow.showAtLocation(findViewById(R.id.linear), Gravity.RIGHT|Gravity.BOTTOM, 0,0);
20 }else{
21 boo=true;
22 mPopupWindow.dismiss();
23 }
24
25 }});
26 }
27
28 private void check(){
29 if(mPopupWindow==null){
30 mPopupWindow=new PopupWindow(getLayoutInflater().inflate(R.layout.controler, null),LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
31 }
32 if(mPopupWindow.isShowing()){
33 mPopupWindow.dismiss();
34 }
35 }
36 }
如果要响应PopupWindow里的widget,可以用LayoutInflater过滤xml文件,再用View view=getLayoutInflater().inflate(resource, root);就可以了。
原文地址:https://www.cnblogs.com/etgyd/p/2009747.html