2013-09-26 118 views
1

我有一个线性布局,我想用XML文件中从右到左填充一个元素。从右向左填充的原因是%的舍入误差,因为我想匹配右侧的布局。从右到左的Android线性布局

一种是单独为:55% - 15% - 15% - 15%

另一种是:49% - 2% - 34% - 15%

所有我想要的是正确的15%的是完全一样的,因为现在它是不一样的(也许1px的或2px的差异)。

我尝试了方向和重力,但没有得到理想的结果。

编辑:

对不起,这里是我的代码:

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:baselineAligned="false" 
     android:weightSum="100"> 
     <RelativeLayout 
      android:id="@+id/leftSideDefaultHoldeTitleBar" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="49" 
      android:background="@android:color/holo_green_dark"> 
     </RelativeLayout> 
     <RelativeLayout 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="2" 
      android:background="@android:color/darker_gray"> 

     </RelativeLayout> 
     <RelativeLayout 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="34" 
      android:background="@android:color/black"> 

     </RelativeLayout> 
     <RelativeLayout 
       android:layout_width="0px" 
       android:layout_height="match_parent" 
       android:layout_weight="15" 
       android:background="@android:color/blue" 
      > 
     </RelativeLayout> 
    </LinearLayout> 

其他布局是一样的只是在重量的其他价值观,我有摊位放在同一持有人,当我切换他们之间在右侧是一个小错误。

+0

这将有助于看到您的布局或代码中添加UI元素。 – Dave

+0

先发布你的'xml'。我们会很高兴地纠正它! –

回答

1

对LinearLayout使用weightSum = 100,并根据您指定的百分比为所有子视图设置权​​重。

+0

对主布局的所有孩子使用layout_weight,并根据您需要的百分比设置孩子。例如,如果您在主布局ser layout_weight = 0.5中有两个布局,则两者都将同时占据整个主布局的两部分。 – newBie

1

试试这个,我觉得它完全一样。见所附快照

[<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="5dp" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="100dp" 
     android:weightSum="100" > 

     <TextView 
      android:background="#f00" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="55" 
      android:gravity="center" 
      android:text="55" 
      android:textSize="18sp" /> 

     <TextView 
      android:background="#f00f" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="15" 
      android:gravity="center" 
      android:text="15" 
      android:textSize="18sp" /> 

     <TextView 
      android:background="#ff00ff" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="15" 
      android:gravity="center" 
      android:text="15" 
      android:textSize="18sp" /> 

     <TextView 
      android:background="#0f000f" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="15" 
      android:gravity="center" 
      android:text="15" 
      android:textSize="18sp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="100dp" 
     android:layout_marginTop="5dp" 
     android:weightSum="100" > 

     <TextView 
      android:background="#f00" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="49" 
      android:gravity="center" 
      android:text="55" 
      android:textSize="18sp" /> 

     <TextView 
      android:background="#f00f" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="2" 
      android:gravity="center" 
      android:text="15" 
      android:textSize="18sp" /> 

     <TextView 
      android:background="#ff00ff" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="34" 
      android:gravity="center" 
      android:text="15" 
      android:textSize="18sp" /> 

     <TextView 
      android:background="#0f000f" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="15" 
      android:gravity="center" 
      android:text="15" 
      android:textSize="18sp" /> 
    </LinearLayout> 

</LinearLayout> 

enter image description here

+0

我没有在模拟器上运行它,我在galaxy S2上运行,并不一样。因为所有的自定义视图,我不会海图形我的布局 – user2704821

+0

我试过这个,它的运行良好。您是否在设备上尝试过上述布局? –

+2

没有其他的方式可以在布局中做到这一点。 –