2013-07-31 55 views
1

我有一個警告對話框,其中包含一個正面和負面的按鈕,以編程方式添加。在對話框佈局中,AlertDialog的原生按鈕上方還有兩個按鈕。Android - 使AlertDIalog按鈕具有統一尺寸

dialog

當這兩個緊挨着彼此,我意識到,在此對話框中,天然正/負/中性按鈕未相等地加權。他們的文本內容決定了他們的水平權重,而不是每個對話框的50%(如果3個按鈕爲33%)。所以在我的情況下,負面按鈕的文本比正面按鈕的文本長,我們得到的東西看起來像我上面張貼的圖像。

我找不到解決這個問題的方法,有沒有人有想法?

謝謝!根據要求

XML:

<?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> 
+1

你介意分享你的佈局xml嗎? – Peshal

+0

當然,但xml不是導致按鈕權重不均勻的原因,這是來自AlertDialog類 – JMRboosties

+0

,您可以嘗試在提交按鈕標題(如「Submit」或「」)之前,之後或之前和之後添加空格提交「'。這有點草率,但它可以幫助您調整重量。 – John

回答

9

想出了一個解決方案......不知道爲什麼我之前沒想到它。

我做到以下幾點:

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); 
    } 
}); 

基本上,你必須控制按鈕的尺寸之後所示的對話框。所以創建一個onShowListener,你可以通過這種方式設置權重和寬度。感謝您的評論,我希望這可以幫助未來的人。