Android alertdialog的自定义添加按钮和添加点击
2016-08-24 11:42
375 查看
我要做的是一个竖排选择的按钮,上面要有图片,因为才学安卓不久小白一个,本来是做ios开发的因公司需要- -,正好也感兴趣,但是感觉学的好慢,就慢慢总结吧,
先看效果图。(左边是图片,右边是文字)
下面是代码:
因为实在是太小白了,不确定alertdialog的宽度的话,它会随着title 的长度而改变宽度,考虑到多语言的问题,只能固定最大宽度。
下面是xml文件,一个是布局xml。
先看效果图。(左边是图片,右边是文字)
下面是代码:
TableLayout choose = (TableLayout) getLayoutInflater().inflate(R.layout.choose_purchase_way,null); AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.AlertDialog); // builder.setView(LayoutInflater.from(this).inflate(R.layout.choose_purchase_way,null)); builder.setView(choose); builder.setTitle("Choose Payment Method"); // builder.setTitle("请选择支付方式"); AlertDialog alertDialog = builder.create(); Button Button1 = (Button) choose.findViewById(R.id.ib_select1Pay); Button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LogUtils.e("AlertDialog","select 1"); } }); Button Button2 = (Button) choose.findViewById(R.id.ib_select2Pay); Button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LogUtils.e("AlertDialog","select 2"); } }); alertDialog.show(); // alertDialog.getWindow().setLayout(600,800); //上面的方法也可以修改alertdialog 的大小,但是我只需要宽度固定,所以采用下面的方法。 WindowManager.LayoutParams lp= alertDialog.getWindow().getAttributes(); lp.width=850;//定义宽度 // lp.height=800;//定义高度 alertDialog.getWindow().setAttributes(lp);
因为实在是太小白了,不确定alertdialog的宽度的话,它会随着title 的长度而改变宽度,考虑到多语言的问题,只能固定最大宽度。
下面是xml文件,一个是布局xml。
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/mol_white"> <Button android:id="@+id/ib_select1Pay" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="30dp" android:drawablePadding="-10dp" android:drawableLeft="@drawable/1_logo" android:text="****" /> <Button android:id="@+id/ib_select2Pay</span>" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="30dp" android:drawablePadding="-30dp" android:drawableLeft="@drawable/2_logo" android:text="*****" /> </TableLayout>
android:background="@color/mol_white" 只能设置 内容部分的背景色,
<pre name="code" class="html">android:paddingLeft="30dp" 设置图片距离左边边框的距离
<pre name="code" class="html">android:drawablePadding="-30dp" 文字距离左边图片的距离<span style="white-space:pre"> </span>
下面是style的xml文件,这里是改变 alertdialog的样式的关键
<pre name="code" class="html"><style name="AlertDialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog" tools:targetApi="ice_cream_sandwich"> <!-- 这里设置背景为透明,为了隐藏边框 --> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> <!-- 这里是修改顶部标题背景颜色,具体颜色自己定,可以是图片 --> <item name="android:topDark">@color/mol_white</item> <!-- 这里是修改内容区域背景颜色 --> <item name="android:centerDark">@color/mol_white</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> </style>
<pre name="code" class="csharp"><pre name="code" class="csharp">最后在说一下,安卓的像素问题,= =一开始设置的时候是按照ios 的像素配置的,我擦,好坑爹,调了好久才发现这个问题。示例中,红色部分左边是图片,右边是文字。一共是两个按钮。查资料查的好辛苦,自己太笨。基于Android studio。
相关文章推荐
- android开发之AlertDialog点击按钮之后不消失
- android开发之AlertDialog点击按钮之后不消失
- ALertDialog自定义View对话框 获得View上的输入信息 + 点击按钮满足条件后消失
- Android自定义样式(风格)的dialog对话框(点击其中的按钮跳转到下一个页面)
- Android AlertDialog解决点击按钮立即消失的问题
- Android实现点击AlertDialog上按钮时不关闭对话框的方法
- Android 自定义的Dialog,点击确定按钮,让其根据条件判断消失与否
- Android之在Dialog中添加单选按钮【自定义Dialog】
- Android:实现点击AlertDialog上按钮时不关闭对话框
- Android Alert Dialog解决点击按钮对话框不关闭/的问题
- Android alertdialog的按钮点击后不消失
- android开发之AlertDialog点击按钮之后不消失 分类: android 学习笔记 2015-07-15 18:07 89人阅读 评论(0) 收藏
- android alertdialog的按钮点击后不消失
- Android:实现点击AlertDialog上按钮时不关闭对话框
- android点击AlertDialog上的按钮后让其不消失
- Android Studio AlertDialog 为对话框添加按钮
- Android实现点击AlertDialog上按钮时不关闭对话框的方法
- Android实现点击AlertDialog上按钮时不关闭对话框
- Android实现点击AlertDialog上按钮时不关闭对话框的方法
- Android alertdialog的按钮点击后不消失