您的位置:首页 > 移动开发 > Android开发

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

2018-02-08 11:45 851 查看

本文实例讲述了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 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相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

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

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息