2013-03-04 101 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