2012-07-28 87 views
0

我意識到這已被問過無數次,但我還沒有爲自己想出一個解決方案。我想製作一組簡單的按鈕,如下所示,不使用GridLayout。我也沒有太多的運氣與TableLayout或RelativeLayout。什麼工作對我來說,和好,是的LinearLayout:均勻佈局,無嵌套權重

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:padding="5dp" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#d0b0b0" 
      android:paddingRight="10dp" 
      android:textSize="15dip" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="0dp" 
      android:background="#808080" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#a09a09" 
      android:textSize="15dip" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:padding="5dp" > 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#456456" 
      android:padding="10dp" 
      android:textSize="15dip" /> 
    </LinearLayout> 

</LinearLayout> 

但我發現了關於「嵌套的權重是壞的表現」的警告。真?有這麼簡單的佈局?我可以忽略警告嗎?有沒有其他(優雅?)的方式來做到這一點?

LinearLayout, 3 buttons

+0

以及我想說無視警告,繼續前進。還有最後一個按鈕中的'android:layout_weight =「1」',你不需要。 – 0gravity 2012-07-28 04:24:46

+0

當我拿出最後一個按鈕的'android:layout_weight =「1」'時,按鈕消失! – coco 2012-07-29 03:43:11

+0

那麼,如果您將寬度保留爲「0dp」,它當然會消失。你必須把在「match_parent」 – 0gravity 2012-07-29 04:27:45

回答

1

你可以避開一些調整,以你目前的佈局文件嵌套佈局警告:

<?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" 
    android:padding="5dp" > 

    <View 
     android:id="@+id/anchor" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_centerVertical="true" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/anchor" 
     android:layout_alignParentTop="true" 
     android:padding="5dp" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#d0b0b0" 
      android:paddingRight="10dp"    
      android:textSize="15dip" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="0dp" 
      android:background="#808080" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#a09a09"    
      android:textSize="15dip" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_below="@id/anchor" 
     android:orientation="horizontal" 
     android:padding="5dp" > 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#456456" 
      android:padding="10dp"    
      android:textSize="15dip" /> 
     <!-- textSize should be set in sp units, like 15sp --> 
    </LinearLayout> 

</RelativeLayout> 

此外,您可能想要刪除它包裝單ButtonLinearLayout(並添加5分空白到Button)。

+0

這會爲我生成一個空白屏幕 – coco 2012-07-31 18:41:32

+0

@coco對不起,檢查我編輯的答案,它應該現在就好了。 – Luksprog 2012-07-31 19:24:24

+0

謝謝@Lukesprog。 DeeV對於他提供的鏈接也是如此 - 這個答案的變體,更多解釋。 – coco 2012-07-31 20:37:13

0

有關android佈局的另一個有趣的細節,它也具有去除嵌套權重上的lint警告的效果。如果我把我的按鈕到合併文件這樣的:

<merge xmlns:android="http://schemas.android.com/apk/res/android" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#d0b0b0" 
     android:textSize="15dip" /> 

</merge> 

..做同樣的其他按鈕(和間隔認爲我做了),然後包括這些合併XML文件到像主xml文件這:

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:padding="5dp" > 

     <include layout="@layout/button1" /> 

     <include layout="@layout/spacer" /> 

     <include layout="@layout/button2" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:padding="5dp" > 

     <include layout="@layout/button3" /> 
    </LinearLayout> 

</LinearLayout> 

..我失去了所有的皮棉警告! lint是否錯過了這個問題,還是這樣更容易膨脹?

它不僅使它更容易閱讀(對我而言),它給了我很多重用的可能性。 I < 3通過XML佈局!

+0

我很確定這只是林特沒有發現的東西,而且你仍然有相同的佈局處罰。 – 2015-03-15 19:21:41

2

更新:我們通過android支持庫得到了非常棒的解決方案,將我們的佈局平分爲百分比。

避免完全混亂LinearLayout weights

compile 'com.android.support:percent:23.0.0' 

Code and concept

Github project

考慮這個簡單的佈局。

percent layout demo

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:id="@+id/fifty_huntv" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:background="#ff7acfff" 
     android:text="20% - 50%" 
     android:textColor="@android:color/white" 
     app:layout_heightPercent="20%" 
     app:layout_widthPercent="50%" /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_toRightOf="@id/fifty_huntv" 
     android:background="#ffff5566" 
     android:text="80%-50%" 
     app:layout_heightPercent="80%" 
     app:layout_widthPercent="50%" 
     /> 

</android.support.percent.PercentRelativeLayout> 

賓果我們done.Really真棒:-)