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

我要做的是一个竖排选择的按钮,上面要有图片,因为才学安卓不久小白一个,本来是做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。
 
 

 
 
 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值