這篇文章將為大家詳細(xì)講解有關(guān)Android中怎么利用ButtomDialog自定義底部彈出框,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)主營(yíng)贛縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件定制開(kāi)發(fā),贛縣h5重慶小程序開(kāi)發(fā)公司搭建,贛縣網(wǎng)站營(yíng)銷推廣歡迎贛縣等地區(qū)企業(yè)咨詢
一 、先來(lái)配置自定義控件需要的資源
1.在res文件夾下創(chuàng)建一個(gè)anim文件夾并創(chuàng)建兩個(gè)slide_in_bottom.xml、slide_out_bottom.xml文件,負(fù)責(zé)彈框進(jìn)出動(dòng)畫(huà)。
<?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添加陰影和動(dòng)畫(huà)樣式。
<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)建一個(gè)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); //添加條目,可多個(gè) builder.addMenu("相機(jī)", new View.OnClickListener() { @Override public void onClick(View view) { dialog.cancel(); Toast.makeText(MainActivity.this, "相機(jī)", Toast.LENGTH_SHORT).show(); } }).addMenu("相冊(cè)", new View.OnClickListener() { @Override public void onClick(View view) { dialog.cancel(); Toast.makeText(MainActivity.this, "相冊(cè)", Toast.LENGTH_SHORT).show(); } }); //下面這些設(shè)置都可不寫(xiě) builder.setTitle("這是標(biāo)題");//添加標(biāo)題 builder.setCanCancel(false);//點(diǎn)擊陰影時(shí)是否取消dialog,true為取消 builder.setShadow(true);//是否設(shè)置陰影背景,true為有陰影 builder.setCancelText("取消");//設(shè)置最下面取消的文本內(nèi)容 //設(shè)置點(diǎn)擊取消時(shí)的事件 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; } }}
關(guān)于Android中怎么利用ButtomDialog自定義底部彈出框就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
本文標(biāo)題:Android中怎么利用ButtomDialog自定義底部彈出框
文章地址:http://jinyejixie.com/article32/poeepc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、服務(wù)器托管、網(wǎng)站制作、小程序開(kāi)發(fā)、域名注冊(cè)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)