Android自定义对话框

Android自定义对话框

01.程式碼

01 Dialog dialog = new Dialog(MenuShow.this,R.style.MyDialog);//指定自定義樣式
02 dialog.setContentView(R.layout.dialog);//指定自定義layout
03  
04 //可*調整佈局內部元件的屬性
05 LinearLayout ll = (LinearLayout)dialog.findViewById(R.id.lldialog);
06 ll.getLayoutParams().width=360;
07  
08 Window dialogWindow = dialog.getWindow();
09 WindowManager.LayoutParams lp = dialogWindow.getAttributes();
10 //dialogWindow.setGravity(Gravity.BOTTOM | Gravity.RIGHT);
11 lp.x = 500; // 新位置X坐標
12 lp.y = 450; // 新位置Y坐標
13 lp.width = 100; // 寬度
14 lp.height = 100; // 高度
15 lp.alpha = 0.7f; // 透明度
16  
17 //新增自定義按鈕點擊監聽
18 Button btn = (Button)dialog.findViewById(R.id.dialog_button_ok);
19 btn.setOnClickListener(new OnClickListener() {
20      
21     @Override
22     public void onClick(View v) {
23          
24     }
25 });
26  
27 //顯示dialog
28 dialog.show();

02.styles.xml增加

1 <style name="MyDialog" parent="@android:Theme.Dialog">
2      <item name="android:windowFrame">@null</item>
3      <item name="android:windowNoTitle">true</item>
4      <item name="android:windowBackground">@drawable/dialog_full</item>
5      <item name="android:windowIsFloating">true</item>
6      <item name="android:windowContentOverlay">@null</item>
7  </style>

03.自定義layout

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03      
04     android:orientation="vertical"
05     android:layout_width="wrap_content"
06     android:layout_height="wrap_content"
07     android:gravity="center_vertical|center_horizontal"
08     >
09  
10     <LinearLayout
11         android:id="@+id/lldialog"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         android:orientation="vertical" >
15  
16         <LinearLayout
17             android:layout_width="match_parent"
18             android:layout_height="wrap_content"
19             android:background="@drawable/dialog_up"
20             android:orientation="vertical"
21             android:paddingBottom="10dp"
22             android:paddingTop="10dp" >
23  
24             <TextView
25                 android:id="@+id/textView1"
26                 android:layout_width="wrap_content"
27                 android:layout_height="wrap_content"
28                 android:layout_gravity="center_horizontal"
29                 android:text="店名" />
30  
31             <TextView
32                 android:id="@+id/textView2"
33                 android:layout_width="wrap_content"
34                 android:layout_height="wrap_content"
35                 android:layout_gravity="center_horizontal"
36                 android:text="地址" />
37         </LinearLayout>
38  
39         <LinearLayout
40             android:layout_width="match_parent"
41             android:layout_height="wrap_content"
42             android:background="@drawable/dialog_dn"
43             android:gravity="center"
44             android:padding="10dp"
45             android:paddingBottom="10dp" >
46  
47             <Button
48                 android:id="@+id/dialog_button_cancel"
49                 android:layout_width="0dp"
50                 android:layout_height="51dip"
51                 android:layout_weight="1"
52                 android:background="@drawable/btn_bg_gray"
53                 android:text="取消"
54                 android:textColor="#FFFFFF" />
55  
56             <Button
57                 android:id="@+id/dialog_button_ok"
58                 android:layout_width="0dp"
59                 android:layout_height="51dp"
60                 android:layout_marginLeft="10dp"
61                 android:layout_weight="1"
62                 android:background="@drawable/btn_bg_green"
63                 android:text="確定"
64                 android:textColor="#FFFFFF" />
65         </LinearLayout>
66  
67     </LinearLayout>
68 </LinearLayout>

 

 

http://www.dotblogs.com.tw/superlm102/archive/2013/02/05/90118.aspx

Android自定义对话框,布布扣,bubuko.com

Android自定义对话框

上一篇:Android Sqlite数据库加密


下一篇:[日常] 安装windows+deepin双系统