android自定義Dialog彈框和背景陰影顯示效果
本文實(shí)例為大家分享了android自定義Dialog彈框和背景陰影顯示的具體代碼,供大家參考,具體內(nèi)容如下

首先需要自定義一個類,繼承Dialog
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import com.zhiziyun.dmptest.bot.R;
/**
* Created by Administrator on 2018/1/31.
*/
public class CustomDialog extends Dialog {
private Button yes, no;//確定按鈕
private TextView titleTv;//消息標(biāo)題文本
private TextView messageTv;//消息提示文本
private String titleStr;//從外界設(shè)置的title文本
private String messageStr;//從外界設(shè)置的消息文本
//確定文本和取消文本的顯示內(nèi)容
private String yesStr, noStr;
private onNoOnclickListener noOnclickListener;//取消按鈕被點(diǎn)擊了的監(jiān)聽器
private onYesOnclickListener yesOnclickListener;//確定按鈕被點(diǎn)擊了的監(jiān)聽器
/**
* 設(shè)置取消按鈕的顯示內(nèi)容和監(jiān)聽
*
* @param str
* @param onNoOnclickListener
*/
public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
if (str != null) {
noStr = str;
}
this.noOnclickListener = onNoOnclickListener;
}
/**
* 設(shè)置確定按鈕的顯示內(nèi)容和監(jiān)聽
*
* @param str
* @param onYesOnclickListener
*/
public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
if (str != null) {
yesStr = str;
}
this.yesOnclickListener = onYesOnclickListener;
}
public CustomDialog(Context context) {
super(context, R.style.Dialog_Msg);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_custom);
//按空白處不能取消動畫
setCanceledOnTouchOutside(false);
//初始化界面控件
initView();
//初始化界面數(shù)據(jù)
initData();
//初始化界面控件的事件
initEvent();
}
/**
* 初始化界面的確定和取消監(jiān)聽器
*/
private void initEvent() {
//設(shè)置確定按鈕被點(diǎn)擊后,向外界提供監(jiān)聽
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener != null) {
yesOnclickListener.onYesClick();
}
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener != null) {
noOnclickListener.onNoClick();
}
}
});
}
/**
* 初始化界面控件的顯示數(shù)據(jù)
*/
private void initData() {
//如果用戶自定了title和message
if (titleStr != null) {
titleTv.setText(titleStr);
}
if (messageStr != null) {
messageTv.setText(messageStr);
}
//如果設(shè)置按鈕的文字
if (yesStr != null) {
yes.setText(yesStr);
}
}
/**
* 初始化界面控件
*/
private void initView() {
yes = (Button) findViewById(R.id.yes);
no = (Button) findViewById(R.id.no);
titleTv = (TextView) findViewById(R.id.title);
messageTv = (TextView) findViewById(R.id.message);
}
/**
* 從外界Activity為Dialog設(shè)置標(biāo)題
*
* @param title
*/
public void setTitle(String title) {
titleStr = title;
}
/**
* 從外界Activity為Dialog設(shè)置dialog的message
*
* @param message
*/
public void setMessage(String message) {
messageStr = message;
}
/**
* 設(shè)置確定按鈕和取消被點(diǎn)擊的接口
*/
public interface onYesOnclickListener {
public void onYesClick();
}
public interface onNoOnclickListener {
public void onNoClick();
}
@Override
public void show() {
super.show();
/**
* 設(shè)置寬度全屏,要設(shè)置在show的后面
*/
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height= ViewGroup.LayoutParams.MATCH_PARENT;
getWindow().getDecorView().setPadding(0, 0, 0, 0);
getWindow().setAttributes(layoutParams);
}
}
這是實(shí)體類中的style:
<style name="custom_dialog_style" parent="android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">false</item> <item name="android:windowNoTitle">true</item><!--除去title--> <item name="android:backgroundDimEnabled">true</item><!--半透明--> <item name="android:windowBackground">@color/transparent</item><!--除去背景色--> <item name="android:radius">10dp</item> </style>
其中@color/transparent是一個透明色
<color name="transparent">#00000000</color>
然后是布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A5000000">
<LinearLayout
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/shape_dialog_msg"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="15dp"
android:gravity="center"
android:text="消息提示"
android:textColor="@color/colorBlack"
android:textSize="@dimen/title_text_size" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/padding_left_right4"
android:layout_marginRight="@dimen/padding_left_right4"
android:text="提示消息"
android:textColor="@color/colorBlack"
android:textSize="@dimen/textsizi3" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="15dp"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/buttom_height"
android:orientation="horizontal">
<Button
android:id="@+id/no"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:gravity="center"
android:singleLine="true"
android:text="取消"
android:textColor="@color/blue"
android:textSize="@dimen/textsizi3" />
<Button
android:id="@+id/yes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:gravity="center"
android:singleLine="true"
android:text="確 定"
android:textColor="@color/red"
android:textSize="@dimen/textsizi3" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
下面是shape_dialog_msg的代碼
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"> <shape android:shape="rectangle" > <!-- 填充的顏色 前兩位是透明度--> <solid android:color="#f7f6f6"></solid> <!-- 設(shè)置按鈕的四個角為弧形 --> <!-- android:radius 弧形的半徑 --> <corners android:radius="8dip" /> <!-- padding:Button里面的文字與Button邊界的間隔 --> <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp" /> </shape> </item> </selector>
準(zhǔn)備工作都做好了,下面就是如何使用了
//點(diǎn)擊彈出對話框
final CustomDialog customDialog = new CustomDialog(getActivity());
customDialog.setTitle("消息提示");
customDialog.setMessage("是否暫停廣告投放?");
customDialog.setYesOnclickListener("確定", new CustomDialog.onYesOnclickListener() {
@Override
public void onYesClick() {
//這里是確定的邏輯代碼,別忘了點(diǎn)擊確定后關(guān)閉對話框
customDialog.dismiss();
}
});
customDialog.setNoOnclickListener("取消", new CustomDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
customDialog.dismiss();
}
});
customDialog.show();
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義Dialog的2種常見方法
- Android自定義Dialog框樣式
- Android自定義Dialog原理實(shí)例解析
- Android 自定義加載動畫Dialog彈窗效果的示例代碼
- Android自定義底部彈出框ButtomDialog
- Android自定義Dialog實(shí)現(xiàn)通用圓角對話框
- Android自定義dialog 自下往上彈出的實(shí)例代碼
- Android 自定義Dialog去除title導(dǎo)航欄的解決方法
- Android自定義Dialog實(shí)現(xiàn)加載對話框效果
- Android編程自定義AlertDialog樣式的方法詳解
- 解決Android中自定義DialogFragment解決寬度和高度問題
- Android?自定義?Dialog?實(shí)現(xiàn)列表?單選、多選、搜索功能
相關(guān)文章
Android 中ImageView的ScaleType使用方法
這篇文章主要介紹了Android 中ImageView的ScaleType使用方法的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下2017-09-09
Android Broadcast 和 BroadcastReceiver的權(quán)限限制方式
這篇文章主要介紹了Android Broadcast 和 BroadcastReceiver的權(quán)限限制方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Kotlin Extension Function擴(kuò)展函數(shù)詳細(xì)介紹
Kotlin支持使用新功能擴(kuò)展類的能力,而無需通過類實(shí)現(xiàn)繼承概念或使用設(shè)計(jì)模式,如裝飾器(Decorator)。這是通過稱為擴(kuò)展功能(Extension Function)的特殊方式來完成的。因此,此功能可以有效地使代碼變得更清晰和易于閱讀,并且還可以減少代碼2023-02-02
android開發(fā)仿ios的UIScrollView實(shí)例代碼
下面小編就為大家分享一篇android開發(fā)仿ios的UIScrollView實(shí)例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android基于service實(shí)現(xiàn)音樂的后臺播放功能示例
這篇文章主要介紹了Android基于service實(shí)現(xiàn)音樂的后臺播放功能,結(jié)合實(shí)例形式分析了Android基于Service組件實(shí)現(xiàn)多媒體音頻播放功能的步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android開發(fā)使用PopupMenu創(chuàng)建彈出式菜單完整實(shí)例
這篇文章主要介紹了Android開發(fā)使用PopupMenu創(chuàng)建彈出式菜單,結(jié)合完整實(shí)例形式分析了Android基于PopupMenu對象創(chuàng)建的彈出式菜單相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-03-03
android app跳轉(zhuǎn)應(yīng)用商店實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了android app跳轉(zhuǎn)應(yīng)用商店實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Android TreeView實(shí)現(xiàn)帶復(fù)選框樹形組織結(jié)構(gòu)
這篇文章主要為大家詳細(xì)介紹了Android TreeView實(shí)現(xiàn)帶復(fù)選框樹形組織結(jié)構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07

