2016-11-27 83 views
1

我試圖使用LinearLayout爲了將3個按鈕放在一行中。 左側按鈕(固定尺寸)應該與屏幕左側對齊,右側按鈕(固定尺寸)與右側對齊。中間按鈕的大小應該是可變的並且應該填充剩餘的空間。以下代碼的結果是,右側按鈕超出界限且不可見。任何人都可以幫我解決這個問題嗎?使用LinearLayout的按鈕超出限制

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:orientation="horizontal" 
    android:weightSum="1.0"> 

    <Button 
    android:id="@id/LeftButton" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:elevation="9dp" 
    android:focusable="true" 
    android:paddingBottom="10dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="10dp" 
    android:layout_weight="0.2"/> 

    <Button 
    android:id="@id/MiddleButton" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="15dp" 
    android:paddingTop="18dp" 
    android:layout_weight="0.6"/> 

    <Button 
    android:id="@id/RightButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.2"/> 

</LinearLayout> 

回答

0

您應該將中間按鈕寬度更改爲0dp,這將縮放它以適應缺少的部分。

您可以更改佈局:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:orientation="horizontal"> 

    <Button 
    android:id="@id/LeftButton" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:elevation="9dp" 
    android:focusable="true" 
    android:paddingBottom="10dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="10dp"/> 

    <Button 
    android:id="@id/MiddleButton" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:paddingLeft="15dp" 
    android:paddingTop="18dp" 
    android:layout_weight="1"/> 

    <Button 
    android:id="@id/RightButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

</LinearLayout> 
+0

非常感謝快速反應,遺憾的是中間的按鈕是現在已經完全「隱藏」 ... – Florian

+0

也許屏幕不夠大? 從按鈕中刪除填充並查看會發生什麼 – fbwnd

0

使用此,它會給你你所需要的。

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:clickable="true" 
android:focusable="true" 
android:focusableInTouchMode="true" 
android:orientation="horizontal" 
android:weightSum="1.0"> 

    <Button 
    android:id="@+id/LeftButton" 
    android:layout_width="0dp" 
    android:layout_height="50dp" 
    android:elevation="9dp" 
    android:focusable="true" 
    android:paddingBottom="10dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="10dp" 
    android:layout_weight="0.2"/> 

<Button 
    android:id="@+id/MiddleButton" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:paddingLeft="15dp" 
    android:paddingTop="18dp" 
    android:layout_weight="0.6"/> 

    <Button 
    android:id="@+id/RightButton" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.2"/> 

</LinearLayout> 
0

您應該更改所有按鈕的寬度,當你設置layout_weight和LinearLayout中上水平,以0dp。

enter image description here

+1

我已將相同的東西放在我的答案pouria中。 – CopsOnRoad

+0

當我們使用linearlayout vertical並設置layout_weight那麼我們應該設置layout_height = 0dp。 –

+0

而當我們使用linearlayout horizo​​ntal並設置layout_weight那麼我們應該設置layout_height = 0dp。 –