android自定義Dialog彈框和背景陰影顯示效果
本文實例為大家分享了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;//取消按鈕被點擊了的監(jiān)聽器 private onYesOnclickListener yesOnclickListener;//確定按鈕被點擊了的監(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è)置確定按鈕被點擊后,向外界提供監(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è)置確定按鈕和取消被點擊的接口 */ 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); } }
這是實體類中的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)備工作都做好了,下面就是如何使用了
//點擊彈出對話框 final CustomDialog customDialog = new CustomDialog(getActivity()); customDialog.setTitle("消息提示"); customDialog.setMessage("是否暫停廣告投放?"); customDialog.setYesOnclickListener("確定", new CustomDialog.onYesOnclickListener() { @Override public void onYesClick() { //這里是確定的邏輯代碼,別忘了點擊確定后關(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原理實例解析
- Android 自定義加載動畫Dialog彈窗效果的示例代碼
- Android自定義底部彈出框ButtomDialog
- Android自定義Dialog實現(xiàn)通用圓角對話框
- Android自定義dialog 自下往上彈出的實例代碼
- Android 自定義Dialog去除title導(dǎo)航欄的解決方法
- Android自定義Dialog實現(xiàn)加載對話框效果
- Android編程自定義AlertDialog樣式的方法詳解
- 解決Android中自定義DialogFragment解決寬度和高度問題
- Android 自定義 Dialog 實現(xiàn)列表 單選,多選,搜索功能
相關(guān)文章
OKhttp攔截器實現(xiàn)實踐環(huán)節(jié)源碼解析
這篇文章主要為大家介紹了OKhttp攔截器實現(xiàn)實踐環(huán)節(jié)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01android通過jxl讀excel存入sqlite3數(shù)據(jù)庫
本文主要介紹了android通過jxl去讀excel的內(nèi)容,然后存入sqlite3數(shù)據(jù)庫表,需要用到j(luò)xl的jar包和sqlite 的jar包,圖片是excel的數(shù)據(jù)格式,需要的朋友可以參考下2014-03-03Android編程實現(xiàn)Gallery中每次滑動只顯示一頁的方法
這篇文章主要介紹了Android編程實現(xiàn)Gallery中每次滑動只顯示一頁的方法,涉及Android擴(kuò)展Gallery控件實現(xiàn)翻頁效果控制的功能,涉及Android事件響應(yīng)及屬性控制的相關(guān)技巧,需要的朋友可以參考下2015-11-11Material Design系列之自定義Behavior支持所有View
這篇文章主要為大家詳細(xì)介紹了Material Design系列之自定義Behavior支持所有View,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Android在ubuntu上過濾多條關(guān)鍵字日志
今天小編就為大家分享一篇關(guān)于Android在ubuntu上過濾多條關(guān)鍵字日志,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04Android開發(fā)實現(xiàn)的Intent跳轉(zhuǎn)工具類實例
這篇文章主要介紹了Android開發(fā)實現(xiàn)的Intent跳轉(zhuǎn)工具類,簡單描述了Intent組件的功能并結(jié)合實例形式給出了頁面跳轉(zhuǎn)、拍照、圖片調(diào)用等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Android單一實例全局可調(diào)用網(wǎng)絡(luò)加載彈窗
這篇文章主要為大家詳細(xì)介紹了Android單一實例全局可調(diào)用網(wǎng)絡(luò)加載彈窗,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12