Android的 - 让AlertDIalog按钮的大小均匀 [英] Android - Make AlertDIalog buttons a uniform size

查看:500
本文介绍了Android的 - 让AlertDIalog按钮的大小均匀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有编程增加了一个正面和负面的按钮警告对话框。在该对话框的布局也有两个按钮,上面的AlertDialog的本机的按钮。

在这两个紧挨着对方,我意识到,在此对话框中,本机正/负/中性按钮不相同的权重。其文本的内容决定了他们的水平权重,而不是每次服用同比增长50%(或者3个按钮33%)对话框。所以在我的情况下,如果负按钮的文本比正面按钮的文本较长,我们得到的东西,看起来像我上面张贴的形象。

我无法找到一个方法来解决这个问题,没有任何人有一个想法?

谢谢!

XML的要求:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent>    < TextView的机器人:ID =@ + ID / rcd_msg
              机器人:layout_width =WRAP_CONTENT
              机器人:layout_height =WRAP_CONTENT
              机器人:layout_centerHorizo​​ntal =真
              机器人:layout_margin =8DP
              机器人:文字=@字符串/ rating_dialog_text
              机器人:TEXTSIZE =14dp
              机器人:文字颜色=@机器人:彩色/白
              机器人:比重=中心/>    <的RatingBar机器人:ID =@ + ID / rcd_rating_bar
               机器人:layout_width =WRAP_CONTENT
               机器人:layout_height =WRAP_CONTENT
               机器人:layout_below =@ ID / rcd_msg
               机器人:layout_margin =8DP
               机器人:paddingBottom会=10dp
               机器人:numStars =5
               机器人:等级=0
               机器人:layout_centerHorizo​​ntal =真/>    < RelativeLayout的机器人:layout_height =56dp
                    机器人:layout_width =match_parent
                    机器人:layout_below =@ ID / rcd_rating_bar>        <的LinearLayout的android:layout_width =match_parent
                      机器人:layout_height =match_parent
                      机器人:方向=横向>            < RelativeLayout的机器人:ID =@ + ID / rcd_image
                            机器人:layout_width =0dp
                            机器人:layout_height =FILL_PARENT
                            机器人:背景=@彩色/ selectable_transparent
                            机器人:layout_weight =1>                < TextView的机器人:ID =@ + ID / rcd_image_text
                          机器人:layout_height =WRAP_CONTENT
                          机器人:layout_width =WRAP_CONTENT
                          机器人:layout_centerInParent =真
                          机器人:paddingLeft =0dp
                          机器人:比重=中心
                          机器人:文字=@字符串/ add_image
                          机器人:文字颜色=@机器人:彩色/白
                          机器人:TEXTSIZE =16SP
                          机器人:可点击=真/>                < ImageView的机器人:ID =@ + ID / rcd_image_img
                           机器人:layout_height =32dp
                           机器人:layout_width =32dp
                           机器人:layout_centerVertical =真
                           机器人:layout_margin =8DP
                           机器人:layout_toLeftOf =@ ID / rcd_image_text
                           机器人:scaleType =centerInside
                           机器人:SRC =@机器人:可绘制/ ic_menu_camera/>            < / RelativeLayout的>            < RelativeLayout的机器人:ID =@ + ID / rcd_comment
                            机器人:layout_width =0dp
                            机器人:layout_height =FILL_PARENT
                            机器人:背景=@彩色/ selectable_transparent
                            机器人:layout_weight =1>                < TextView的机器人:ID =@ + ID / rcd_comment_text
                          机器人:layout_height =WRAP_CONTENT
                          机器人:layout_width =WRAP_CONTENT
                          机器人:layout_centerInParent =真
                          机器人:paddingRight =6DP
                          机器人:比重=中心
                          机器人:文字=@字符串/ add_comment
                          机器人:文字颜色=@机器人:彩色/白
                          机器人:TEXTSIZE =16SP
                          机器人:可点击=真/>                < ImageView的机器人:ID =@ + ID / rcd_comment_img
                           机器人:layout_height =32dp
                           机器人:layout_width =32dp
                           机器人:layout_centerVertical =真
                           机器人:layout_margin =4DP
                           机器人:layout_toRightOf =@ ID / rcd_comment_text
                           机器人:scaleType =centerInside
                           机器人:SRC =@绘制/ ic_menu_comment/>            < / RelativeLayout的>        < / LinearLayout中>        <的LinearLayout的android:layout_width =1DP
                      机器人:layout_height =38dp
                      机器人:layout_centerHorizo​​ntal =真
                      机器人:layout_alignParentBottom =真
                      机器人:方向=横向
                      机器人:layout_margin =10dp
                      机器人:背景=@色/ transparent_white/>    < / RelativeLayout的>< / RelativeLayout的>


解决方案

想出一个解决方案......不知道为什么我之前没想到它。

我做到以下几点:

  mRateConcertDialog.setOnShowListener(新OnShowListener(){    @覆盖
    公共无效昂秀(DialogInterface D){
        按钮posButton = mRateConcertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        按钮negButton = mRateConcertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);        的LayoutParams posParams =(的LayoutParams)posButton.getLayoutParams();
        posParams.weight = 1;
        posParams.width = LayoutParams.MATCH_PARENT;        的LayoutParams negParams =(的LayoutParams)negButton.getLayoutParams();
        negParams.weight = 1;
        negParams.width = LayoutParams.MATCH_PARENT;        posButton.setLayoutParams(posParams);
        negButton.setLayoutParams(negParams);
    }
});

基本上,你必须操作按钮的大小后所示的对话框。因此,创建一个onShowListener,你可以只设置的权重和宽度的方式。感谢您的意见,我希望这可以帮助别人的未来。

I have an alert dialog with a positive and negative button added programatically. In the dialog layout there are also two buttons, above the AlertDialog's native buttons.

When these two are right next to each other, I realized that in this dialog, the native positive/negative/neutral buttons are not equally weighted. The content of their text determines their horizontal weight, rather than each taking up 50% (or 33% if 3 buttons) of the dialog. So in my case, where the negative button's text is longer than that positive button's text, we get something that looks like the image I posted above.

I can't find a way to fix this, does anyone have an idea?

Thanks!

XML as requested:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

    <TextView android:id="@+id/rcd_msg"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_margin="8dp"
              android:text="@string/rating_dialog_text"
              android:textSize="14dp"
              android:textColor="@android:color/white"
              android:gravity="center"/>

    <RatingBar android:id="@+id/rcd_rating_bar"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_below="@id/rcd_msg"
               android:layout_margin="8dp"
               android:paddingBottom="10dp"
               android:numStars="5"
               android:rating="0"
               android:layout_centerHorizontal="true"/>

    <RelativeLayout android:layout_height="56dp"
                    android:layout_width="match_parent"
                    android:layout_below="@id/rcd_rating_bar">

        <LinearLayout android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:orientation="horizontal">

            <RelativeLayout android:id="@+id/rcd_image"
                            android:layout_width="0dp"
                            android:layout_height="fill_parent"
                            android:background="@color/selectable_transparent"
                            android:layout_weight="1">

                <TextView android:id="@+id/rcd_image_text"
                          android:layout_height="wrap_content"
                          android:layout_width="wrap_content"
                          android:layout_centerInParent="true"
                          android:paddingLeft="0dp"
                          android:gravity="center"
                          android:text="@string/add_image"
                          android:textColor="@android:color/white"
                          android:textSize="16sp"
                          android:clickable="true"/>

                <ImageView android:id="@+id/rcd_image_img"
                           android:layout_height="32dp"
                           android:layout_width="32dp"
                           android:layout_centerVertical="true"
                           android:layout_margin="8dp"
                           android:layout_toLeftOf="@id/rcd_image_text"
                           android:scaleType="centerInside"
                           android:src="@android:drawable/ic_menu_camera"/>

            </RelativeLayout>

            <RelativeLayout android:id="@+id/rcd_comment"
                            android:layout_width="0dp"
                            android:layout_height="fill_parent"
                            android:background="@color/selectable_transparent"
                            android:layout_weight="1">

                <TextView android:id="@+id/rcd_comment_text"
                          android:layout_height="wrap_content"
                          android:layout_width="wrap_content"
                          android:layout_centerInParent="true"
                          android:paddingRight="6dp"
                          android:gravity="center"
                          android:text="@string/add_comment"
                          android:textColor="@android:color/white"
                          android:textSize="16sp"
                          android:clickable="true"/>

                <ImageView android:id="@+id/rcd_comment_img"
                           android:layout_height="32dp"
                           android:layout_width="32dp"
                           android:layout_centerVertical="true"
                           android:layout_margin="4dp"
                           android:layout_toRightOf="@id/rcd_comment_text"
                           android:scaleType="centerInside"
                           android:src="@drawable/ic_menu_comment"/>

            </RelativeLayout>

        </LinearLayout>

        <LinearLayout android:layout_width="1dp"
                      android:layout_height="38dp"
                      android:layout_centerHorizontal="true"
                      android:layout_alignParentBottom="true"
                      android:orientation="horizontal"
                      android:layout_margin="10dp"
                      android:background="@color/transparent_white" />

    </RelativeLayout>

</RelativeLayout>

解决方案

Figured out a solution... don't know why I didn't think of it before.

I do the following:

mRateConcertDialog.setOnShowListener(new OnShowListener() {

    @Override
    public void onShow(DialogInterface d) {
        Button posButton = mRateConcertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        Button negButton = mRateConcertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);

        LayoutParams posParams = (LayoutParams) posButton.getLayoutParams();
        posParams.weight = 1;
        posParams.width = LayoutParams.MATCH_PARENT;

        LayoutParams negParams = (LayoutParams) negButton.getLayoutParams();
        negParams.weight = 1;
        negParams.width = LayoutParams.MATCH_PARENT;

        posButton.setLayoutParams(posParams);
        negButton.setLayoutParams(negParams);
    }
});

Basically, you have to manipulate the size of the buttons AFTER the dialog is shown. So create an onShowListener and you can just set weights and widths that way. Thanks for the comments, I hope this can help someone in the future.

这篇关于Android的 - 让AlertDIalog按钮的大小均匀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆