首页 > 系统 > Android > 正文

Android编程自定义AlertDialog样式的方法详解

2019-10-22 18:15:25
字体:
来源:转载
供稿:网友

本文实例讲述了Android编程自定义AlertDialog样式的方法。分享给大家供大家参考,具体如下:

开发的时候,通常我们要自定义AlertDialog来满足我们的功能需求:

比如弹出对话框中可以输入信息,或者要展示且有选择功能的列表,或者要实现特定的UI风格等。那么我们可以通过以下方式来实现。

方法一:完全自定义AlertDialog的layout.如我们要实现有输入框的AlertDialog布局custom_dialog.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:background="@drawable/dialog_bg"  android:orientation="vertical">  <TextView    android:id="@+id/title"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="#00ffff"    android:gravity="center"    android:padding="10dp"    android:text="Dialog标题"    android:textSize="18sp" />  <EditText    android:id="@+id/dialog_edit"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:hint="请输入内容"    android:minLines="2"    android:textScaleX="16sp" />  <LinearLayout    android:layout_width="match_parent"    android:layout_height="40dp"    android:orientation="horizontal">    <Button      android:id="@+id/btn_cancel"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:layout_weight="1"      android:background="#00ffff"      android:text="取消" />    <View      android:layout_width="1dp"      android:layout_height="40dp"      android:background="#D1D1D1"></View>    <Button      android:id="@+id/btn_comfirm"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:layout_weight="1"      android:background="#00ffff"      android:text="确定" />  </LinearLayout></LinearLayout>

原来在代码中使用:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);View view = View    .inflate(getActivity(), R.layout.custom_dialog, null);builder.setView(view);builder.setCancelable(true);TextView    .findViewById(R.id.title);//设置标题EditText input_edt= (EditText) view    .findViewById(R.id.dialog_edit);//输入内容Button btn_cancel=(Button)view.findViewById(R.id.btn_cancel);//取消按钮 Button btn_comfirm=(Button)view.findViewById(R.id.btn_comfirm);//确定按钮//取消或确定按钮监听事件处理AlertDialog dialog = builder.create();dialog.show();

这样,我们就可以弹出一个我们自定义的Dialog。这种方式有个弊端就是:

如果项目中有多个UI不同的AlertDialog,我们要写多个布局页面,当然可以提取通用布局,然后各种处理。

方法2:通过修改 Android 系统原生的 AlertDialog 中的控件来达到我们想要的效果。

比如我们要实现特定风格的对话框,我们可以写个公共的方法,通过修改 Android 系统原生的 AlertDialog 中的控件来达到我们想要的效果,简单代码如下:

public static void setCustomDialogStyle(AlertDialog dialog){final Resources res = dialog.getContext().getResources();    int topPanelId = res.getIdentifier("topPanel", "id", "android");//获取顶部    LinearLayout topPanel = (LinearLayout) getDialog().findViewById(topPanelId);    topPanel.setBackgroundResource(R.drawable.dialog_top_bg);//设置顶部背景    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, //设置顶部高度        dp2px(getDialog().getContext(), 50));    topPanel.setLayoutParams(params);    int dividerId = res.getIdentifier("titleDivider", "id", "android");//设置分隔线    View divider = getDialog().findViewById(dividerId);    divider.setVisibility(View.GONE);    int titleId = res.getIdentifier("alertTitle", "id", "android");//获取标题title    TextView    title.setTextColor(Color.WHITE);//标题文字颜色    title.setTextSize(18);//文字大小    title.setGravity(Gravity.CENTER);//文字位置    int customPanelId = res.getIdentifier("customPanel", "id", "android");//设置内容    FrameLayout customPanel = (FrameLayout) getDialog().findViewById(customPanelId);    customPanel.setBackgroundColor(Color.TRANSPARENT);//背景透明    customPanel.getChildAt(0).setBackgroundColor(Color.WHITE);    customPanel.setPadding(dp2px(getContext(), 8), 0, ViewUtils.dp2px(getContext(), 8), 0);//设置padding    int buttonPanelId = res.getIdentifier("buttonPanel", "id", "android");//获取底部    LinearLayout buttonPanel = (LinearLayout) getDialog().findViewById(buttonPanelId);    buttonPanel.setBackgroundResource(R.drawable.dialog_bottom_bg);//设置底部背景    buttonPanel.setPadding(dp2px(getContext(), 8), 1, dp2px(getContext(), 8), 0);    Button button1 = (Button) getDialog().findViewById(android.R.id.button1);//设置底部Button    button1.setTextColor(Color.WHITE);//文字颜色    button1.setTextSize(18);//文字大小    button1.setBackgroundResource(R.drawable.bg_right_round);//Button圆形背景框    Button button2 = (Button) getDialog().findViewById(android.R.id.button2);    button2.setTextColor(Color.WHITE);    button2.setTextSize(18);    button2.setBackgroundResource(R.drawable.bg_left_round);}

代码中用到的各种颜色,背景图片等根据需求自己定义。用到的dp与px转换代码如下:

public static int dp2px(Context context, float dp) {    float density = context.getResources().getDisplayMetrics().density;    return (int) (dp * density + 0.5f);}

这样我们就统一定义好了AlertDialog的整个界风格,在使用的时候,只需要根据UI需求定义内容部分的UI即可。

还是上面可以输入的AlertDialog,我们的布局就可以只写成下面这个,当然,外面层的LinearLayout也是可以去掉的。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_content"  android:layout_height="wrap_content"  android:orientation="vertical">  <EditText    android:id="@+id/input_edt"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_margin="10dp"    android:background="@drawable/input"    android:hint="请输入内容"    android:maxLength="16"    android:textColor="#333333"    android:textSize="16sp" /></LinearLayout>

然后在代码中使用:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder.setTitle("设置标题");View view = View    .inflate(getActivity(), R.layout.custom_dialog, null);builder.setView(view);builder.setCancelable(true);EditText input_edt= (QRCodeEditText) view    .findViewById(R.id.input_edt);builder.setPositiveButton(android.R.string.ok,    new DialogInterface.OnClickListener() {      @Override      public void onClick(DialogInterface dialog, int which) {        //点击确定按钮处理          dialog.cancel();        }      }    });builder.setNegativeButton(android.R.string.cancel,    new DialogInterface.OnClickListener() {      @Override      public void onClick(DialogInterface dialog, int which) {      //点击取消按钮处理        dialog.cancel();      }    });final AlertDialog dialog = builder.create();dialog.show();setCustomDialogStyle(dialog);//这里不要忘记调用setCustomDialogStyle方法

这种方式 就比第一种方式方便 多了。当然要实现AlertDialog的背景透明等效果,我们还可以在res/value/style.xml内增加以下代码:

<style name="dialog" parent="@android:style/Theme.Dialog">     <item name="android:windowFrame">@null</item> //Dialog的windowFrame框为无     <item name="android:windowIsFloating">true</item> //是否浮现在activity之上     <item name="android:windowIsTranslucent">true</item> //是否半透明     <item name="android:windowNoTitle">true</item> //是否显示title     <item name="android:background">@android:color/transparent</item> //设置dialog的背景     <item name="android:windowBackground">@android:color/transparent</item>     <item name="android:backgroundDimAmount">0.7</item> //就是用来控制灰度的值,当为1时,界面除了我们的dialog内容是高亮显示的,dialog以外的区域是黑色的,完全看不到其他内容     <item name="android:backgroundDimEnabled">true</item></style>

在需要加入alertDialog的地方加入以下语句:

AlertDialog.Builder alertbBuilder=new AlertDialog.Builder(getActivity(),R.style.dialog);//接下来代码.....

希望本文所述对大家Android程序设计有所帮助。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表