2011-03-28 93 views
26

我想在屏幕中間顯示三個按鈕,並且三個按鈕都是相同的寬度,儘管它們將具有不同長度的文本標籤。如何創建等寬的按鈕?

只需添加三個帶不同長度文本標籤的按鈕,即可生成不同寬度的按鈕。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center"> 
    <Button 
     android:id="@+id/button_1" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:text="ABCDEF" /> 
    <Button 
     android:id="@+id/button_2" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:text="GHI" /> 
    <Button 
     android:id="@+id/button_3" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:text="JKLM" /> 
</LinearLayout>

默認按鈕寬度包裝內容:
default button width wraps contents

-

設置layout_weight爲1,layout_width到0dip上的所有按鈕使它們同等地拉伸以填充整個屏幕寬度。對於我想要的,這些按鈕太大了,特別是在大屏幕上。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center"> 
    <Button 
     android:id="@+id/button_1" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="ABCDEF" /> 
    <Button 
     android:id="@+id/button_2" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="GHI" /> 
    <Button 
     android:id="@+id/button_3" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="JKLM" /> 
</LinearLayout>

佈局重量1按鈕填充屏幕寬度:
layout weight 1 buttons fill screen width

-

在父的LinearLayout設置爲weightSum不同的值可用於從充滿整個屏幕停止按鈕,但我不認爲這是我想要採取的路徑,因爲我不希望按鈕佔用大屏幕設備的大部分屏幕。爲了說明一下,使用weightSum,例如,我可以將三個按鈕設置爲佔用屏幕寬度的一半,這在小屏幕上可能看起來不錯,但在大屏幕上,按鈕仍佔用屏幕寬度的一半,按鈕會比我想要的大得多。也許最終的解決方案是簡單地爲不同的屏幕設置不同的佈局文件,但我寧願不走這條道路。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:gravity="center" 
    android:weightSum="5"> 
    <Button 
     android:id="@+id/button_1" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="ABCDEF" /> 
    <Button 
     android:id="@+id/button_2" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="GHI" /> 
    <Button 
     android:id="@+id/button_3" 
     android:layout_height="fill_parent" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:text="JKLM" /> 
</LinearLayout>

權重和5個小屏:
weight sum 5 small screen

權重和5大屏:
weight sum 5 large screen

-

我也試圖與TableLayout很多東西,但沒」沒有比使用LinearLayout更好的了。

GridView是非常笨拙的使用,我還沒有嘗試過。

那麼,如何創建具有相同寬度的按鈕,最好是它們的寬度必須儘可能寬,以適應按鈕內容最長的標籤?

任何意見表示讚賞。

(我做了搜索和發現這個問題,提出和回答很多次,但沒有我發現解決了我想要達到的答案。)

+1

如何寫一個自定義佈局? – bigstones 2011-03-28 13:35:42

回答

5

也許最終的解決方案將是簡單地有不同屏幕的不同佈局文件,但我寧願不走這條道路。

許多程序員會使用res/layout/res/layout-large/處理這樣的情況。在三個按鈕的有限情況下,您可能有其他選擇,但通常用戶界面並不那麼簡單。

那麼,如何創建具有相同寬度的按鈕,最好是隻在必要的範圍內儘可能寬以適應具有最長標籤的按鈕內容?

要完成您的「優選」[原文]需求,您需要爲此創建自定義佈局類。 Here is one related to it,對於儀表板模式,您可能將其用作起點。

+0

Arg。 [抱怨接受Android不會做我想做的事。看起來很遺憾,有一個功能用於設置按鈕具有相同的高度,但不具有相同的寬度(不使用權重)。] – 2011-04-09 18:37:43

-1

閱讀此線程,然後做一些嘗試和錯誤,並注意到android:layout_width="180px"是一個公認的參數。如前所述,我只是偶然發現了這個問題,並沒有試圖用這個來解決你的三個按鈕場景。

自從您最初發布以來,事情可能發生了變化。儘管我在爲1.5版本構建時嘗試了這一點。這就夠老了...... :-)

下面是完整的main.xml

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

    <TextView 
     android:id="@+id/dateText" 
     android:text="\n\nClick for Date\n\n" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="180px" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="Get the Time" 
     android:onClick="showNewDate" /> 

</LinearLayout> 
+0

1.您應該使用'dp'而不是'px'。 2.將寬度設置爲特定的dp量並不能保證按鈕內的文本適合(然後取決於文本)。 – 2013-07-31 11:28:50

3

如果你事先知道你的最廣泛的按鈕上的文字會是什麼,你可以使用android:ems屬性來設置你的按鈕,以便寬度。

<Button 
    android:id="@+id/my_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:minEms="5" 
    android:text="@string/my_button" /> 

這不是完美的,但它是最簡單的方法,我發現實現這種外觀沒有無休止地擺弄佈局。

0

只需要我一邊的附加說明!

如果您需要設置在運行時添加的按鈕(或代碼的簡單更改的任何其他視圖)的寬度(或高度),您可以使用下面的代碼(它不取決於方向):

public void AlignButtons(){ 
    llModules = (LinearLayout) findViewById(R.id.llModules); 

    ViewTreeObserver viewTree = llModules.getViewTreeObserver(); 
    viewTree.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
     public boolean onPreDraw() { 

      int childcount = llModules.getChildCount(); 
      int maxWidth = 0; //int maxHeight = 0; 

      String fontPath = "fonts/Isabella-Decor.ttf"; 
      Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); 

      for (int i = 0; i < childcount; i++) { 
       LinearLayout v = (LinearLayout)llModules.getChildAt(i); 
       View vv = v.getChildAt(0); 
       if (vv instanceof Button) { 
        int width = vv.getMeasuredWidth(); 
        maxWidth = (maxWidth > width) ? maxWidth : width; 
        //int height = vv.getMeasuredHeight(); 
        //maxHeight = (maxHeight > height) ? maxHeight : height; 
       } 
      } 
      for (int i = 0; i < childcount; i++) { 
       LinearLayout v = (LinearLayout)llModules.getChildAt(i); 
       View vv = v.getChildAt(0); 
       if (vv instanceof Button) { 
        LayoutParams params = ((Button) vv).getLayoutParams(); 
        params.width = maxWidth; 
        //params.height = maxHeight; 
        ((Button) vv).setLayoutParams(params); 

        // Applying font 
        ((Button) vv).setTypeface(tf); 
       } 
       vv = v.getChildAt(1); 
       if (vv instanceof TextView) { 
        // Applying font 
        ((TextView) vv).setTypeface(tf); 
       } 
      } 

      return true; 
     } 
    }); 
} 

在這裏,在我的情況:

llModule - 一些佈局包含什麼,我們需要對齊(v.getChildAt(0);另一個佈局)和其他(​​)。

LinearLayout v =(LinearLayout)llModules.getChildAt(i); - 獲取要對齊的按鈕佈局。

其他部分很明白。

這段代碼中的兩個週期是完全相同的,但我仍然無法想象如何組合它們(以加快執行時間)。

0

使用此正確的代碼,這將幫助你。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:weightSum="3"> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Get the Time" 
    android:onClick="showNewDate" /> 
<Button 
    android:id="@+id/button2" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Get the Time" 
    android:onClick="showNewDate" /> 
<Button 
    android:id="@+id/button3" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Get the Time" 
    android:onClick="showNewDate" />