2011-02-13 61 views
82

我是Android開發新手,我有一個關於在線性佈局中設置重量的問題。Android佈局重量

我想創建一個具有兩個自定義按鈕和自定義編輯文本的行。編輯文本應該只佔用與內容相同的空間,並且這兩個按鈕應該水平擴展以填充行中剩餘的可用空間。像這樣...

| [#Button++#] [#Button--#] [et] | 

多次嘗試後,這是最接近我能得到我想要的東西,即使它似乎過於複雜。

| [Button] [Button] [###ET###] | 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal|center_vertical" 
    android:layout_weight="1" 
    > 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center_horizontal|center_vertical" 
     > 
     <RelativeLayout 
      android:id="@+id/btn_plus_container" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      > 
      <ImageButton 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent" 
       android:id="@+id/btn_plus" 
       android:background="@drawable/btn" 
       android:layout_centerInParent="true" 
       ></ImageButton> 
      <TextView 
       android:textColor="#FAFAF4" 
       android:id="@+id/tv_dice_plus" 
       android:text="@string/tv_plus" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       ></TextView> 
     </RelativeLayout> 
    </LinearLayout> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center_horizontal|center_vertical" 
     > 
     <RelativeLayout 
      android:id="@+id/btn_minus_container" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      > 
      <ImageButton 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent" 
       android:id="@+id/btn_minus" 
       android:background="@drawable/btn" 
       android:layout_centerInParent="true" 
       ></ImageButton> 
      <TextView 
       android:textColor="#FAFAF4" 
       android:id="@+id/btn_minus" 
       android:text="@string/tv_minus" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       ></TextView> 
     </RelativeLayout> 
    </LinearLayout> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center_horizontal|center_vertical" 
     > 
     <EditText 
      android:textColor="#FAFAF4" 
      android:text="@string/et_output" 
      android:id="@+id/et_output" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="number" 
      android:selectAllOnFocus="true" 
      android:minWidth="50sp"   
      android:maxWidth="50sp" 
      android:background="@drawable/tv_black_background3" 
      android:textColorHighlight="#c30505" 
      ></EditText> 
    </LinearLayout> 
</LinearLayout> 

據我所知的設置[機器人:layout_weight =「2」]用於保持按鈕線性佈局應使得它們佔用比編輯文本更多的空間,但它確實對置對我來說,使他們都成爲一半。

| [Btn] [Btn] [#####et#####] | 

我已經嘗試過很多不同的組合,我什至不記得他們,也沒有工作。

回答

211

,因爲你正在使用FILL_PARENT爲寬度它不工作。當總和大於LinearLayout時,權重用於分配剩餘的空白空間佔用空間。相反,它的寬度設置爲0dip它將起作用。

+6

設置的線性佈局的寬度以0dip的工作就像我想要的。我不知道這是一個選擇。這是有道理的,爲什麼這將工作在wrap_content不行的地方。在沒有內容的情況下將寬度設置爲wrap_content會使其消失,但將其設置爲0dip可以使其從無到有,但需要大。非常感謝你。 – UnluckyDevil 2011-02-15 07:01:03

0

我會想給EditText小號寬度設置爲wrap_content並把兩個按鈕進入LinearLayout,其寬度爲fill_parent和重量設定爲1

2

體重值0-1分配的可用空間(在設置layout_width =「0px」之後)與體重值成比例。查看重量未指定的元素(沒有重量輸入)的重量爲0,這意味着它們沒有擴展。

與不需要重條目的簡單的替代方法是選取框附加到與文本 一個視圖,它告訴它從所需的文本(WRAP_CONTENT)到可用空間 的EditText最小擴展: 機器人:layout_width =「WRAP_CONTENT」 安卓:ellipsize當你沒有一個固定值,附加到您的寬度已經像fill_parent等等=「跑馬燈」

14

android:Layout_weight可用於

做這樣的事情:

<Button> 

Android:layout_width="0dp" 
Android:layout_weight=1 

-----other parameters 
</Button> 
7

認爲這樣的說法,會更簡單

如果你有3個按鈕和權重分別爲1,3,1。因此,它會像表HTML

該行提供5份:按鈕1的部分1,按鈕2的3部分和按鈕1的1部分

0

我發現的另一個原因(模糊,因爲它聽起來)。下面沒有工作。

的LinearLayout垂直

的LinearLayout高度fillparent +重量

的LinearLayout高度fillparent +重量

的LinearLayout高度fillparent +重量

EndLinearLayout

什麼做的工作是

的RelativeLayout

的LinearLayout垂直

的LinearLayout高度fillparent +重量

的LinearLayout高度fillparent +重量

的LinearLayout高度fillparent +重量

EndLinearLayout

EndRelativeLayout

這聽起來由根佈局與線性模糊,其下的權重沒有工作。而當我說「不起作用」時,我的意思是說,在查看了各種分辨率之間的圖形佈局後,屏幕一致性打破了很大的時間。

3

在linearLayout中設置WeightSum = 2;

並將重量分配給它的孩子,因爲你希望他們顯示..我已經給孩子稱重=「1」,所以兩者將分配總數的一半。

<LinearLayout 
    android:id="@+id/linear1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="2" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/ring_oss" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/ring_oss" /> 

    <ImageView 
     android:id="@+id/maila_oss" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/maila_oss" /> 
</LinearLayout> 
0
<LinearLayout 
    android:id="@+id/linear1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="9" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/ring_oss" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="3" 
     android:src="@drawable/ring_oss" /> 

    <ImageView 
     android:id="@+id/maila_oss" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="3" 
     android:src="@drawable/maila_oss" /> 
<EditText 
     android:id="@+id/edittxt" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="3" 
     android:src="@drawable/maila_oss" /> 
</LinearLayout>