高速公路合肥中标单位:弹出小窗口,PopupWindow的使用

来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 19:13:51
弹出小窗口,PopupWindow的使用

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

1 
2  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 
2   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 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 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 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 32 android:id="@+id/seekbar"
33 android:layout_width="500dip"
34 android:layout_height="wrap_content"
35 android:max="10000"
36 />
37 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 45 android:id="@+id/soundup"
46 android:layout_width="wrap_content"
47 android:layout_height="wrap_content"
48 android:src="@drawable/soundup"
49 />
50

51 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
60
61

最后就是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);就可以了。