2013-03-04 102 views
6

我需要設置我的視圖的寬度作爲在屏幕的寬度的50%,然後水平居中同時潛在地具有可出現連接到左或右側的1個或多個按鈕這一觀點屏幕。的Android百分比寬度佈局

我使用的相對佈局,這樣我可以放置一個線性佈局與重量來獲得,同時將在連接到RL的左或右邊緣LL的頂部上的任何按鈕居中我的50%。但是這種佈局缺少藍色的中間欄。如果我將中間視圖layout_weight設置爲1,則會得到3個相同大小的條。

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <LinearLayout 
     android:id="@+id/stupid_android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:layout_weight="1" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#0000FF" 
      android:layout_weight="2" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1" /> 

    </LinearLayout> 

</RelativeLayout> 

回答

15

應查看的寬度設置爲0dip

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <LinearLayout 
     android:id="@+id/stupid_android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <View 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:layout_weight="1" /> 

     <View 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:background="#0000FF" 
      android:layout_weight="2" /> 

     <View 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1" /> 

    </LinearLayout> 

</RelativeLayout> 
+1

這是怎麼了'layout_weight'作品。 'layout_weight'告訴你如何** **剩餘在父空間'View'應該由孩子'View'的devided。在你的情況首先'View'有'安卓layout_width'設置爲'fill_parent'所以沒有任何實際剩餘空間。我不知道爲什麼最後一個視圖仍然可見,因爲我只能看到第一個視圖。 – 2013-03-04 15:16:11

-2

如果我理解正確的話,你需要這樣的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <LinearLayout 
     android:id="@+id/stupid_android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:layout_weight="1" ></View> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#0000FF" 
      android:layout_weight="1.5" ></View> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1.5" ></View> 

    </LinearLayout> 

</RelativeLayout> 
1

您可以用新的百分比庫實現這一點:

https://developer.android.com/tools/support-library/features.html#percent

做這樣的事情:

<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="48dp"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:background="#FF0000" 
     app:layout_widthPercent="25%" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:background="#0000FF" 
     android:centerHorizontal="true" 
     app:layout_marginLeftPercent="25%" 
     app:layout_widthPercent="50%" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:alignParentRight="true" 
     android:background="#00FF00" 
     app:layout_marginLeftPercent="75%" 
     app:layout_widthPercent="25%" /> 

</android.support.percent.PercentRelativeLayout> 
0

使用新的百分比支持庫。

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

例子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <android.support.percent.PercentRelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 

     > 

     <View 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#FF0000" 
      app:layout_widthPercent="33%" /> 

     <View 
      android:layout_height="match_parent" 
      android:layout_weight="2" 
      android:background="#0000FF" 
      app:layout_marginLeftPercent="33%" 
      app:layout_widthPercent="33%" /> 

     <View 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#00FF00" 
      app:layout_marginLeftPercent="66%" 
      app:layout_widthPercent="33%" /> 

    </android.support.percent.PercentRelativeLayout> 


</LinearLayout> 

欲瞭解更多信息Android Doc

*對於樣品&教程* Tutorial1Tutorial2Tutorial3