Android自定義底部彈出框ButtomDialog
本文實(shí)例為大家分享了Android自定義底部彈出框的具體代碼,供大家參考,具體內(nèi)容如下
先看看效果和你要的是否一樣

一 、先來配置自定義控件需要的資源
1.在res文件夾下創(chuàng)建一個anim文件夾并創(chuàng)建兩個slide_in_bottom.xml、slide_out_bottom.xml文件,負(fù)責(zé)彈框進(jìn)出動畫。
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <!-- slide_in_bottom.xml --> <translate android:duration="@integer/dp_300" android:fromXDelta="0%" android:toXDelta="0%" android:fromYDelta="100%" android:toYDelta="0%"/> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <!-- slide_out_bottom.xml --> <translate android:duration="@integer/dp_300" android:fromXDelta="0%" android:toXDelta="0%" android:fromYDelta="0%" android:toYDelta="100%"/> </set>
2.在style.xml添加陰影和動畫樣式。
<style name="Theme.Light.NoTitle.Dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowFrame">@null</item>
</style>
<style name="Theme.Light.NoTitle.NoShadow.Dialog" parent="Theme.Light.NoTitle.Dialog">
<item name="android:backgroundDimEnabled">false</item>
</style>
<style name="Animation.Bottom.Rising" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/slide_in_bottom</item>
<item name="android:windowExitAnimation">@anim/slide_out_bottom</item>
</style>
3.在drawable文件夾下創(chuàng)建一個title_background.xml文件,負(fù)責(zé)給文本內(nèi)容添加背景。
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="8dp"/> <solid android:color="#FFFFFFFF"/> </shape>
二、自定義控件的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="12dp"
>
<LinearLayout
android:background="@drawable/title_background"
android:id="@+id/lay_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:orientation="vertical"/>
<TextView
android:id="@+id/btn_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_background"
android:paddingBottom="8dip"
android:paddingTop="8dip"
android:text="取消"
android:gravity="center"
android:textColor="#007AFF"
android:textSize="17sp"/>
</LinearLayout>
三、自定義控件類
public class ButtomDialog extends Dialog {
public ButtomDialog(Context context, int themeResId) {
super(context, themeResId);
}
public static class Params {
private final List<BottomMenu> menuList = new ArrayList<>();
private View.OnClickListener cancelListener;
private CharSequence menuTitle;
private String cancelText;
private Context context;
}
public static class Builder {
private boolean canCancel = true;
private boolean shadow = true;
private final Params p;
public Builder(Context context) {
p = new Params();
p.context = context;
}
public Builder setCanCancel(boolean canCancel) {
this.canCancel = canCancel;
return this;
}
public Builder setShadow(boolean shadow) {
this.shadow = shadow;
return this;
}
public Builder setTitle(CharSequence title) {
this.p.menuTitle = title;
return this;
}
public Builder addMenu(String text, View.OnClickListener listener) {
BottomMenu bm = new BottomMenu(text, listener);
this.p.menuList.add(bm);
return this;
}
public Builder addMenu(int textId, View.OnClickListener listener) {
return addMenu(p.context.getString(textId), listener);
}
public Builder setCancelListener(View.OnClickListener cancelListener) {
p.cancelListener = cancelListener;
return this;
}
public Builder setCancelText(int resId) {
p.cancelText = p.context.getString(resId);
return this;
}
public Builder setCancelText(String text) {
p.cancelText = text;
return this;
}
public ButtomDialog create() {
final ButtomDialog dialog = new ButtomDialog(p.context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
Window window = dialog.getWindow();
window.setWindowAnimations(R.style.Animation_Bottom_Rising);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
window.setGravity(Gravity.BOTTOM);
View view = LayoutInflater.from(p.context).inflate(R.layout.dialog_bottom_menu, null);
TextView btnCancel = (TextView) view.findViewById(R.id.btn_cancel);
ViewGroup layContainer = (ViewGroup) view.findViewById(R.id.lay_container);
ViewGroup.LayoutParams lpItem = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ViewGroup.MarginLayoutParams lpDivider = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
lpDivider.setMargins(50,0,50,0);
int dip1 = (int) (1 * p.context.getResources().getDisplayMetrics().density + 0.5f);
int spacing = dip1 * 12;
boolean hasTitle = !TextUtils.isEmpty(p.menuTitle);
if (hasTitle) {
//標(biāo)題樣式
TextView tTitle = new TextView(p.context);
tTitle.setLayoutParams(lpItem);
tTitle.setGravity(Gravity.CENTER);
tTitle.setTextColor(p.context.getResources().getColor(R.color.colorAccent));
tTitle.setText(p.menuTitle);
tTitle.setPadding(0, spacing, 0, spacing);
//單獨(dú)給標(biāo)題設(shè)置背景樣式
// tTitle.setBackgroundResource(R.drawable.common_dialog_selection_selector_top);
layContainer.addView(tTitle);
View viewDivider = new View(p.context);
viewDivider.setLayoutParams(lpDivider);
viewDivider.setBackgroundColor(0xFFCED2D6);
layContainer.addView(viewDivider);
}
//每一條的樣式
for (int i = 0; i < p.menuList.size(); i++) {
BottomMenu bottomMenu = p.menuList.get(i);
TextView bbm = new TextView(p.context);
bbm.setLayoutParams(lpItem);
bbm.setPadding(0, spacing, 0, spacing);
bbm.setGravity(Gravity.CENTER);
bbm.setText(bottomMenu.funName);
bbm.setTextColor(0xFF007AFF);
bbm.setTextSize(16);
bbm.setOnClickListener(bottomMenu.listener);
layContainer.addView(bbm);
if (i != p.menuList.size() - 1) {
View viewDivider = new View(p.context);
viewDivider.setLayoutParams(lpDivider);
viewDivider.setBackgroundColor(0xFFCED2D6);
layContainer.addView(viewDivider);
}
}
if (!TextUtils.isEmpty(p.cancelText)) {
btnCancel.setText(p.cancelText);
}
if (p.cancelListener != null) {
btnCancel.setOnClickListener(p.cancelListener);
} else {
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
dialog.setContentView(view);
dialog.setCanceledOnTouchOutside(canCancel);
dialog.setCancelable(canCancel);
return dialog;
}
}
private static class BottomMenu {
public String funName;
public View.OnClickListener listener;
public BottomMenu(String funName, View.OnClickListener listener) {
this.funName = funName;
this.listener = listener;
}
}
}
四、使用
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mDialogCustom;
private ButtomMenuDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mDialogCustom = (Button) findViewById(R.id.custom_dialog);
mDialogCustom.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.custom_dialog:
ButtomMenuDialog.Builder builder = new ButtomMenuDialog.Builder(this);
//添加條目,可多個
builder.addMenu("相機(jī)", new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
Toast.makeText(MainActivity.this, "相機(jī)", Toast.LENGTH_SHORT).show();
}
}).addMenu("相冊", new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
Toast.makeText(MainActivity.this, "相冊", Toast.LENGTH_SHORT).show();
}
});
//下面這些設(shè)置都可不寫
builder.setTitle("這是標(biāo)題");//添加標(biāo)題
builder.setCanCancel(false);//點(diǎn)擊陰影時是否取消dialog,true為取消
builder.setShadow(true);//是否設(shè)置陰影背景,true為有陰影
builder.setCancelText("取消");//設(shè)置最下面取消的文本內(nèi)容
//設(shè)置點(diǎn)擊取消時的事件
builder.setCancelListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
});
dialog = builder.create();
dialog.show();
break;
default:
break;
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義Dialog的2種常見方法
- Android自定義Dialog框樣式
- Android自定義Dialog原理實(shí)例解析
- Android 自定義加載動畫Dialog彈窗效果的示例代碼
- android自定義Dialog彈框和背景陰影顯示效果
- 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的ListView點(diǎn)擊item使item展開的做法的實(shí)現(xiàn)代碼
這篇文章主要介紹了android的ListView點(diǎn)擊item使item展開的做法的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
詳解Android ConstraintLayout 約束布局的用法
本篇文章主要介紹了詳解Android ConstraintLayout 約束布局的用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Android實(shí)現(xiàn)簡單的分批加載ListView
這篇文章主要介紹了Android實(shí)現(xiàn)簡單的分批加載ListView的相關(guān)資料,需要的朋友可以參考下2016-03-03
Flutter實(shí)現(xiàn)自定義搜索框AppBar的示例代碼
開發(fā)中,頁面頭部為搜索樣式的設(shè)計(jì)非常常見,為了可以像系統(tǒng)AppBar那樣使用,本文將利用Flutter自定義一個搜索框,感興趣的可以了解一下2022-04-04
android實(shí)現(xiàn)圖片驗(yàn)證碼方法解析(自繪控件)
本文主要介紹了android自繪控件的應(yīng)用--實(shí)現(xiàn)圖片驗(yàn)證碼方法案例,具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01
android studio 3.6.0 綁定視圖新特性的方法
這篇文章主要介紹了android studio 3.6.0 綁定視圖新特性的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Android實(shí)現(xiàn)自定義華麗的水波紋效果
關(guān)于Android的水波紋效果小編之前給大家也分享幾篇類似的,有興趣可通過下面的相關(guān)文章進(jìn)行查看,今天給大家再分享一個華麗的水波紋效果,這個效果很不錯,感興趣的可以參考借鑒。2016-08-08
Android開發(fā)學(xué)習(xí)筆記之通過API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式
這篇文章主要介紹了Android開發(fā)學(xué)習(xí)筆記之通過API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式的相關(guān)資料,需要的朋友可以參考下2015-11-11

