温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么自定义AlertDialog样式

发布时间:2021-06-28 18:09:10 来源:亿速云 阅读:121 作者:Leah 栏目:移动开发

这期内容当中小编将会给大家带来有关Android中怎么自定义AlertDialog样式,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

方法一:完全自定义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 title= (TextView) view
    .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 = (TextView) getDialog().findViewById(titleId);//设置标题
    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中怎么自定义AlertDialog样式了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI