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

Android alertdialog的自定义添加按钮和添加点击

2016-08-24 11:42 375 查看
我要做的是一个竖排选择的按钮,上面要有图片,因为才学安卓不久小白一个,本来是做ios开发的因公司需要- -,正好也感兴趣,但是感觉学的好慢,就慢慢总结吧,

先看效果图。(左边是图片,右边是文字)



下面是代码:

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 android studio