Android自定義樣式圓角dialog對(duì)話框
本文實(shí)例為大家分享了Android創(chuàng)建自定義樣式圓角dialog對(duì)話框的具體代碼,供大家參考,具體內(nèi)容如下

效果如上,圓角對(duì)話框,標(biāo)題和正文都可以自己設(shè)定
做法:
1.在res文件的layout文件夾創(chuàng)建自己的對(duì)話框布局,命名為my_dialog.xml
2.在res文件的drawable文件夾創(chuàng)建自己的對(duì)話框樣式(圓角),命名為my_dialog_shape.xml
3.寫一個(gè)方法調(diào)用對(duì)話框布局,觸發(fā)條件自定義,這里我是寫了一個(gè)按鈕,在按鈕的點(diǎn)擊事件里調(diào)用方法,彈出對(duì)話框。在這個(gè)方法里可以定義對(duì)話框的標(biāo)題、正文、點(diǎn)擊確定或取消時(shí)觸發(fā)的事件等,還可以設(shè)定對(duì)話框在屏幕上的顯示位置
4.在需要彈出對(duì)話框的地方調(diào)用方法
上代碼:
1.在res文件的layout文件夾創(chuàng)建自己的對(duì)話框布局,命名為my_dialog.xml
對(duì)話框內(nèi)部控件的顯示位置都可以在這里自己調(diào)整
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_marginTop="14dp"
android:gravity="center"
android:layout_gravity="center"
android:id="@+id/title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_marginTop="16dp"
android:layout_marginHorizontal="16dp"
android:gravity="center"
android:layout_gravity="center"
android:id="@+id/message"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"
android:textSize="16sp"
android:textColor="@color/white"
android:background="@null"
android:layout_marginRight="14dp"
android:id="@+id/btn_cancel"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="確定"
android:textSize="16sp"
android:textColor="@color/white"
android:id="@+id/btn_confirm"/>
</LinearLayout>
</LinearLayout>
2.在res文件的drawable文件夾創(chuàng)建自己的對(duì)話框樣式(圓角),命名為my_dialog_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white" />
<corners android:radius="20dp"/>
</shape>
3.寫一個(gè)方法調(diào)用對(duì)話框布局,觸發(fā)條件自定義,這里我是寫了一個(gè)按鈕,在按鈕的點(diǎn)擊事件里調(diào)用方法,彈出對(duì)話框。在這個(gè)方法里可以定義對(duì)話框的標(biāo)題、正文、點(diǎn)擊確定或取消時(shí)觸發(fā)的事件等,還可以設(shè)定對(duì)話框在屏幕上的顯示位置
public void my_dialog(Context context) {
View inflateLayout = LayoutInflater.from(context).inflate(R.layout.my_dialog,null);
TextView unbind_title = (TextView) inflateLayout.findViewById(R.id.title);
unbind_title.setText("標(biāo)題");
TextView unbind_message = (TextView) inflateLayout.findViewById(R.id.message);
unbind_message.setText("正文");
AlertDialog builderDialog = new AlertDialog.Builder(context)
.setView(inflateLayout)
.setCancelable(false) //使用戶只能通過(guò)點(diǎn)擊對(duì)話框的確定或取消關(guān)閉對(duì)話框
.create();
inflateLayout.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "你點(diǎn)擊了確定", Toast.LENGTH_SHORT).show();
builderDialog.dismiss();
}
});
inflateLayout.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "你點(diǎn)擊了取消", Toast.LENGTH_SHORT).show();
builderDialog.dismiss();
}
});
builderDialog.getWindow().setBackgroundDrawableResource(R.drawable.my_dialog_shape); //設(shè)置對(duì)話框的樣式
WindowManager.LayoutParams params = builderDialog.getWindow().getAttributes();
params.y = 1000;
builderDialog.getWindow().setAttributes(params);
builderDialog.show();
builderDialog.getWindow().setGravity(Gravity.TOP); //設(shè)置對(duì)話框展示在距離屏幕頂部1000的位置
}
4.在需要彈出對(duì)話框的地方調(diào)用方法
例如:我在MainActivity里點(diǎn)擊了一下button,觸發(fā)了彈出對(duì)話框的方法
Button pops_up = (Button) findViewById(R.id.pops_up);
pops_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
my_dialog(MainActivity.this);
}
});
代碼完整,歡迎指正
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
RxJava和Retrofit2的統(tǒng)一處理單個(gè)請(qǐng)求示例詳解
這篇文章主要給大家介紹了關(guān)于RxJava和Retrofit2的統(tǒng)一處理單個(gè)請(qǐng)求的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果
本篇文章主要介紹了Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android開發(fā)之圖形圖像與動(dòng)畫(四)AnimationListener簡(jiǎn)介
就像Button控件有監(jiān)聽器一樣,動(dòng)畫效果也有監(jiān)聽器,只需要實(shí)現(xiàn)AnimationListener就可以實(shí)現(xiàn)對(duì)動(dòng)畫效果的監(jiān)聽,感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01
Android 和 windows C/C++/QT通訊時(shí)字節(jié)存儲(chǔ)
本篇文章主要介紹 Android和Windows 通訊時(shí)數(shù)據(jù)地址的理解,這里提供代碼實(shí)例進(jìn)行分析,有需要參考的朋友可以看下2016-07-07
android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放
本篇文章實(shí)現(xiàn)了Android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法
這篇文章主要介紹了Android開發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法,涉及Android activity相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下2017-07-07

