Android中自定義對話框(Dialog)的實例代碼
更新時間:2013年08月23日 16:29:29 作者:
這篇文章介紹了Android中自定義對話框(Dialog)的實例代碼,有需要的朋友可以參考一下
1.修改系統(tǒng)默認的Dialog樣式(風格、主題)
2.自定義Dialog布局文件
3.可以自己封裝一個類,繼承自Dialog或者直接使用Dialog類來實現(xiàn),為了方便以后重復使用,建議自己封裝一個Dialog類
第一步:
我們知道Android定義個控件或View的樣式都是通過定義其style來實現(xiàn)的,查看Android框架中的主題文件,在源碼中的路徑:/frameworks/base/core/res/res/values/themes.xml,我們可以看到,Android為Dialog定義了一個樣式,
<style name="Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
<item name="android:windowBackground">@android:drawable/panel_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
我們可以看到,在Themes.xml中定義的Dialog的樣式,其中,定義了window的標題樣式,window的背景圖,是否懸浮等等。
那么,我們要創(chuàng)建具有自定義樣式的Dialog就可以創(chuàng)建一個styles.xml,在其中定義我們自己的Dialog樣式,讓其繼承自Theme.Dialog樣式,并修改其中的某些屬性即可。
定義我們自己的Dialog樣式:
a.創(chuàng)建一個styles.xml文件,放在res/values 文件夾下(當然了,這就不用說了。。。啰嗦一下)
b.在styles.xml中定義Dialog樣式,代碼如下:
<style name="Theme_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
上面代碼中,定義了一個樣式Theme_dialog,繼承自@android:style/Theme.Dialog,然后定義了Dialog所在Window的背景圖,此處使用的是透明顏色#00000000,然后定義了Dialog所在的Window隱藏標題(系統(tǒng)定義Dialog樣式是帶有標題的,在此設置此屬性為true可隱藏標題),自定義Dialog樣式到這就完成了。
第二步:
定義Dialog的布局:
創(chuàng)建一個布局文件layout_dialog.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/pp_bg_dialog"
android:gravity="center">
<ProgressBar android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/progressbar_normal"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
style="@style/text_white_small" android:layout_marginTop="5dp"
android:text="正在刪除..." android:id="@+id/message"/>
</LinearLayout>
這里使用了一個垂直方向的線性布局,并且設置所有子元素居中,子元素為已個進度條ProgressBar和一個TextView。
此處,ProgressBar采用自定義樣式,使用系統(tǒng)默認的ProgressBar可達到同樣的效果(大同小異)。LinearLayout的背景
android:background="@drawable/pp_bg_dialog"(即上面效果圖中居中顯示的黑色透明背景框)是一個自定義的圖片資源Shape:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#55000000"/>
</shape>
代碼中定義了一個矩形,并設置了圓角和顏色。到此,Dialog的布局就完成了。
第三步:
自定義CustomDialog類,繼承自Dialog,代碼如下:
public class CustomDialog extends Dialog { 2
private static int default_width = 160; //默認寬度
private static int default_height = 120;//默認高度
public CustomDialog(Context context, int layout, int style) {
this(context, default_width, default_height, layout, style);
}
public CustomDialog(Context context, int width, int height, int layout, int style) {
super(context, style);12
//set content
setContentView(layout);
//set window params
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
//set width,height by density and gravity
float density = getDensity(context);
params.width = (int) (width*density);
params.height = (int) (height*density);
params.gravity = Gravity.CENTER;
window.setAttributes(params);
}
private float getDensity(Context context) {
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
return dm.density;
}
}
在構(gòu)造方法中設置了Dialog的contentView,并且設置了Window的寬度、高度和居中顯示。
CustomDialog使用方法如下:
public class CustomDialogDemoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
CustomDialog dialog1 = new CustomDialog(this, R.layout.common_dialog, R.style.Theme_dialog);//Dialog使用默認大小(160)
CustomDialog dialog2 = new CustomDialog(this, 180, 180, R.layout.common_dialog, R.style.Theme_dialog);
dialog2.show();//顯示Dialog
//如果要修改Dialog中的某個View,比如把"正在刪除..."改為"加載中..."
TextView mMessage = (TextView) dialog2.findViewById(R.id.message);
mMessage.setText("加載中...");
}
}
大體過程就是這樣,根據(jù)以上大家可以自由發(fā)揮吧,希望都設計出自己滿意的dialog。
2.自定義Dialog布局文件
3.可以自己封裝一個類,繼承自Dialog或者直接使用Dialog類來實現(xiàn),為了方便以后重復使用,建議自己封裝一個Dialog類
第一步:
我們知道Android定義個控件或View的樣式都是通過定義其style來實現(xiàn)的,查看Android框架中的主題文件,在源碼中的路徑:/frameworks/base/core/res/res/values/themes.xml,我們可以看到,Android為Dialog定義了一個樣式,
復制代碼 代碼如下:
<style name="Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
<item name="android:windowBackground">@android:drawable/panel_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
我們可以看到,在Themes.xml中定義的Dialog的樣式,其中,定義了window的標題樣式,window的背景圖,是否懸浮等等。
那么,我們要創(chuàng)建具有自定義樣式的Dialog就可以創(chuàng)建一個styles.xml,在其中定義我們自己的Dialog樣式,讓其繼承自Theme.Dialog樣式,并修改其中的某些屬性即可。
定義我們自己的Dialog樣式:
a.創(chuàng)建一個styles.xml文件,放在res/values 文件夾下(當然了,這就不用說了。。。啰嗦一下)
b.在styles.xml中定義Dialog樣式,代碼如下:
復制代碼 代碼如下:
<style name="Theme_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
上面代碼中,定義了一個樣式Theme_dialog,繼承自@android:style/Theme.Dialog,然后定義了Dialog所在Window的背景圖,此處使用的是透明顏色#00000000,然后定義了Dialog所在的Window隱藏標題(系統(tǒng)定義Dialog樣式是帶有標題的,在此設置此屬性為true可隱藏標題),自定義Dialog樣式到這就完成了。
第二步:
定義Dialog的布局:
創(chuàng)建一個布局文件layout_dialog.xml,代碼如下:
復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/pp_bg_dialog"
android:gravity="center">
<ProgressBar android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/progressbar_normal"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
style="@style/text_white_small" android:layout_marginTop="5dp"
android:text="正在刪除..." android:id="@+id/message"/>
</LinearLayout>
這里使用了一個垂直方向的線性布局,并且設置所有子元素居中,子元素為已個進度條ProgressBar和一個TextView。
此處,ProgressBar采用自定義樣式,使用系統(tǒng)默認的ProgressBar可達到同樣的效果(大同小異)。LinearLayout的背景
android:background="@drawable/pp_bg_dialog"(即上面效果圖中居中顯示的黑色透明背景框)是一個自定義的圖片資源Shape:
復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#55000000"/>
</shape>
代碼中定義了一個矩形,并設置了圓角和顏色。到此,Dialog的布局就完成了。
第三步:
自定義CustomDialog類,繼承自Dialog,代碼如下:
復制代碼 代碼如下:
public class CustomDialog extends Dialog { 2
private static int default_width = 160; //默認寬度
private static int default_height = 120;//默認高度
public CustomDialog(Context context, int layout, int style) {
this(context, default_width, default_height, layout, style);
}
public CustomDialog(Context context, int width, int height, int layout, int style) {
super(context, style);12
//set content
setContentView(layout);
//set window params
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
//set width,height by density and gravity
float density = getDensity(context);
params.width = (int) (width*density);
params.height = (int) (height*density);
params.gravity = Gravity.CENTER;
window.setAttributes(params);
}
private float getDensity(Context context) {
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
return dm.density;
}
}
在構(gòu)造方法中設置了Dialog的contentView,并且設置了Window的寬度、高度和居中顯示。
CustomDialog使用方法如下:
復制代碼 代碼如下:
public class CustomDialogDemoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
CustomDialog dialog1 = new CustomDialog(this, R.layout.common_dialog, R.style.Theme_dialog);//Dialog使用默認大小(160)
CustomDialog dialog2 = new CustomDialog(this, 180, 180, R.layout.common_dialog, R.style.Theme_dialog);
dialog2.show();//顯示Dialog
//如果要修改Dialog中的某個View,比如把"正在刪除..."改為"加載中..."
TextView mMessage = (TextView) dialog2.findViewById(R.id.message);
mMessage.setText("加載中...");
}
}
大體過程就是這樣,根據(jù)以上大家可以自由發(fā)揮吧,希望都設計出自己滿意的dialog。
您可能感興趣的文章:
- Android自定義對話框Dialog的簡單實現(xiàn)
- Android實現(xiàn)底部對話框BottomDialog彈出實例代碼
- 詳解Android 全局彈出對話框SYSTEM_ALERT_WINDOW權限
- Android實現(xiàn)點擊AlertDialog上按鈕時不關閉對話框的方法
- 實例詳解Android自定義ProgressDialog進度條對話框的實現(xiàn)
- Android 之BottomsheetDialogFragment仿抖音評論底部彈出對話框效果(實例代碼)
- Android實現(xiàn)退出界面彈出提示對話框
- Android中AlertDialog各種對話框的用法實例詳解
- Android仿QQ消息提示實現(xiàn)彈出式對話框
- Android對話框使用方法詳解
相關文章
Android Zygote啟動構(gòu)造流程及進程創(chuàng)建詳解
這篇文章主要為大家介紹了Android Zygote啟動構(gòu)造流程及進程創(chuàng)建詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07Android 調(diào)用系統(tǒng)聯(lián)系人界面(添加聯(lián)系人,添加已有聯(lián)系人,編輯和修改)
這篇文章主要介紹了Android 調(diào)用系統(tǒng)聯(lián)系人界面(添加聯(lián)系人,添加已有聯(lián)系人,編輯和修改),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03詳解Android WebView的input上傳照片的兼容問題
本篇文章主要介紹了詳解Android WebView的input上傳照片的兼容問題,非常具有實用價值,需要的朋友可以參考下2017-08-08Android?中?FrameLayout?布局及屬性的使用詳解
這篇文章主要介紹了Android?中?FrameLayout?布局及屬性的使用,FrameLayout?在實現(xiàn)簡單布局時非常方便,特別適用于疊加式布局,如顯示疊加的圖層或浮動按鈕等,需要的朋友可以參考下2024-03-03Android動態(tài)權限申請實現(xiàn)步驟分解
對于一些危險權限在AndroidManifest清單文件中申請之后,還需要得到用戶的許可并打開,才算是真正的開啟了這個權限。所以可以使用動態(tài)申請權限,對于某個功能,如果需要開啟某個權限,在用戶使用它之前,彈窗提示用戶是否要開啟這個權限2023-04-04Android實現(xiàn)跟隨手指拖動并自動貼邊的View樣式(實例demo)
本文通過實例代碼給大家介紹了android實現(xiàn)跟隨手指拖動并自動貼邊的View樣式,效果非常棒,具有參考借鑒價值,需要的朋友參考下吧2017-01-01Android 開發(fā)中l(wèi)ayout下的子文件夾
這篇文章主要介紹了android 開發(fā)中l(wèi)ayout下的子文件夾,需要的朋友可以參考下2017-12-12Android實現(xiàn)使用流媒體播放遠程mp3文件的方法
這篇文章主要介紹了Android實現(xiàn)使用流媒體播放遠程mp3文件的方法,結(jié)合實例形式分析了Android遠程播放音頻文件的相關步驟與實現(xiàn)技巧,需要的朋友可以參考下2016-08-08