Android之AlertDialog自定义
2016-03-31 17:01
543 查看
AlertDialog的自定义方式有很多种,这里介绍两种。
第一种是比较简单的,只自定义内容。
在AlertDialog使用详解中,非常详细的介绍了以下六种使用方法。
一、简单的AlertDialog(只显示一段简单的信息,比如about us)
二、带按钮的AlertDialog(显示提示信息,让用户操作,比如exit时的警告框)
三、类似ListView的AlertDialog(展示内容,比如某人的一些注册信息)
四、类似RadioButton的AlertDialog(让用户选择,单选)
五、类似CheckBox的AlertDialog(让用户多选)
六、自定义View的AlertDialog(当以上方式满足不了你的需求,就要自定义了)
最后的第六种也就是自定义内容的实现方式,比如想做一个这种登录对话框,通过前五种方式明显实现不了。
这时候就通过AlertDialog.Builder的setView把自己定义的登录界面设置进去,
而标题和按钮都还用Android原生的,如果你是像这种方式的自定义,请进前面链接查看最后一种方式。
这里介绍第二种方式。
先看一下效果,左图为原生的AlertDialog,右图为自定义的AlertDialog,这样自定义主要是为了让界面更加统一。
代码如下:
[java] view
plain copy
print?
String info = cityData.getPointerList().get(position).toString();
AlertDialog alertDialog = new AlertDialog.Builder(CityActivity.this).create();
alertDialog.show();
Window window = alertDialog.getWindow();
window.setContentView(R.layout.dialog_main_info);
TextView tv_title = (TextView) window.findViewById(R.id.tv_dialog_title);
tv_title.setText("详细信息");
TextView tv_message = (TextView) window.findViewById(R.id.tv_dialog_message);
tv_message.setText(info);
布局文件:
[html] view
plain copy
print?
<?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="match_parent"
android:background="#ffff"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#7a7"
android:padding="8dp"
android:textColor="#eee"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_dialog_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="3dp" />
</LinearLayout>
其实这里自定义的主要目的就是想让title的背景颜色显示绿色,与activity的背景绿一致,比较和谐。
第一种是比较简单的,只自定义内容。
在AlertDialog使用详解中,非常详细的介绍了以下六种使用方法。
一、简单的AlertDialog(只显示一段简单的信息,比如about us)
二、带按钮的AlertDialog(显示提示信息,让用户操作,比如exit时的警告框)
三、类似ListView的AlertDialog(展示内容,比如某人的一些注册信息)
四、类似RadioButton的AlertDialog(让用户选择,单选)
五、类似CheckBox的AlertDialog(让用户多选)
六、自定义View的AlertDialog(当以上方式满足不了你的需求,就要自定义了)
最后的第六种也就是自定义内容的实现方式,比如想做一个这种登录对话框,通过前五种方式明显实现不了。
这时候就通过AlertDialog.Builder的setView把自己定义的登录界面设置进去,
而标题和按钮都还用Android原生的,如果你是像这种方式的自定义,请进前面链接查看最后一种方式。
这里介绍第二种方式。
先看一下效果,左图为原生的AlertDialog,右图为自定义的AlertDialog,这样自定义主要是为了让界面更加统一。
代码如下:
[java] view
plain copy
print?
String info = cityData.getPointerList().get(position).toString();
AlertDialog alertDialog = new AlertDialog.Builder(CityActivity.this).create();
alertDialog.show();
Window window = alertDialog.getWindow();
window.setContentView(R.layout.dialog_main_info);
TextView tv_title = (TextView) window.findViewById(R.id.tv_dialog_title);
tv_title.setText("详细信息");
TextView tv_message = (TextView) window.findViewById(R.id.tv_dialog_message);
tv_message.setText(info);
布局文件:
[html] view
plain copy
print?
<?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="match_parent"
android:background="#ffff"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#7a7"
android:padding="8dp"
android:textColor="#eee"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_dialog_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="3dp" />
</LinearLayout>
其实这里自定义的主要目的就是想让title的背景颜色显示绿色,与activity的背景绿一致,比较和谐。
相关文章推荐
- 取消input在ios下,输入的时候英文首字母的默认大写 android 上去掉语音输入按钮
- android-长按Home键,删除历史记录时,service的生命周期
- Android 按键映射 驱动
- Android MyBaseAdapter
- 安卓自定义状态栏颜色
- Android使用procrank和dumpsysmeminfo分析内存占用情况
- Android设置透明、半透明等效果
- Android Context学习
- android studio 获得sha1
- Android源码中增加模块
- android layer-list
- Android 实用工具Hierarchy Viewer实战
- Android判断Touch为滑动事件还是操作控件
- 关于Android ListItem动态更改item UI不更新问题
- Ubuntu 15 安装 Android Studio
- Android虚线不显示
- Android 应用利用反射机制获取/设置系统属性值
- Android Intent传值反馈,向后传递,要求返回的传值
- 在Android中如何通过点击edittext之外的部分使软键盘隐藏
- Android初学习 - Broadcast Receiver的介绍01