AlertDialog使用自定义的布局
来源:互联网 发布:中国网络安全法 编辑:程序博客网 时间:2024/05/09 00:40
Android经常用到对话框—-AlertDialog, 效果图如下:
这是使用系统自带的样式, 使用了建造者模式, Builder添加自己需要显示的item.
代码如下:
new AlertDialog.Builder(this).setTitle("这是标题").setMessage("这是内容").setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // TODO: 点确定的操作 Toast.makeText(TestActivity.this,"确定",Toast.LENGTH_SHORT).show(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // TODO: 点取消的操作 Toast.makeText(TestActivity.this,"取消",Toast.LENGTH_SHORT).show(); } }).create().show();}
但是如果要使用自义的样式就得使用到一个方法: setView(View view).
效果图如下:
下面是代码:
public class TestActivity extends AppCompatActivity implements View.OnClickListener { private AlertDialog mDialog; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn).setOnClickListener(this); } @Override public void onClick(View v) { final AlertDialog.Builder builder = new AlertDialog.Builder(this); View view = View.inflate(this, R.layout.update_dialog, null); ((TextView) view.findViewById(R.id.title_tv)).setText("我是标题"); ((TextView) view.findViewById(R.id.message_tv)).setText("我是内容,虽然样式很丑,但是具体的用法可以使用你自己的漂亮一点的布局"); view.findViewById(R.id.not_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mDialog.dismiss(); } }); view.findViewById(R.id.ok_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO: 点确定的操作 } }); mDialog = builder.setView(view).create(); mDialog.show(); }}
布局代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="180dp" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:padding="10dp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@id/title_tv" android:background="#66515151"/> <TextView android:id="@+id/message_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="4" android:padding="5dp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#66515151"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"> <Button android:id="@+id/not_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" android:background="#fff" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#66505050"/> <Button android:id="@+id/ok_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定" android:background="#fff"/> </LinearLayout></LinearLayout>
1 0
- AlertDialog使用自定义的布局
- 使用AlertDialog自定义布局,解决自定义控件的监听事件无效的办法
- android AlertDialog自定义布局
- 【Android】自定义AlertDialog布局
- 自定义AlertDialog布局
- 自定义AlertDialog对话框布局
- AlertDialog自定义布局
- 自定义AlertDialog布局
- Android AlertDialog自定义布局
- AlertDialog自定义布局
- AlertDialog自定义布局
- 使用自定义视图的AlertDialog
- android AlertDialog自定义布局文件
- android获取自定义布局的alertdialog中edittext的值
- AlertDialog自定义整个布局去掉原有的标题按钮等
- AlertDialog 加载自定义布局点击按钮消失的方法
- alertDialog的自定义style和使用(Android)
- alertDialog的自定义style和使用(Android)
- gcc编译选项和makefile资源汇总
- LeetCode: Top K Frequent Elements
- (XGBoost)提升树入门介绍(Inrtoduction to Boosted Trees)
- English about computer 1
- Django学习6-mysql数据库使用1-安装与连接
- AlertDialog使用自定义的布局
- CSS属性选择器
- 欢迎使用CSDN-markdown编辑器
- windows版本的mysql无法远程连接服务器上的mysql的问题,错误代码10060
- 考研经验分享
- java基础知识学习之代理深入学习(用动态代理实现AOP,FacotryBean)
- 数据结构 栈和队列
- 一起学react native(5) mobx配合FlatList实现高性能im聊天界面
- Android开发最佳学习路线图