2011-09-15 66 views
1

在我的LinearLayout中使用与重力结合的重量会导致重量的影响发生逆转。布局:LinearLayout与重力和重量

<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:layout_weight="0.2"> 
    <TextView android:id="@+id/feedfragmentTitle" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="8"/> 
    <TextView android:id="@+id/feedfragmentDate" android:layout_height="wrap_content" android:gravity="right" android:layout_width="fill_parent" android:layout_weight="2"/> 
</LinearLayout> 

给定的LinearLayout包含一个标题,这应该appromately采取布局的80%和日期+时间与20%。日期+时间应该具有权利的重力。我张贴的布局不起作用,如果我玩参数重量,结果是倒置的。

是否有另一种方式获得结果而不交换权重?

回答

7

当你使用,你需要做适当的高度或宽度0dip

的layout_weight财产试试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView android:id="@+id/feedfragmentTitle" 
     android:layout_height="wrap_content" android:layout_width="0dip" 
     android:layout_weight="8" android:text="sdfdfsfsd"/> 
    <TextView android:text="aass" android:id="@+id/feedfragmentDate" 
     android:layout_height="wrap_content" 
     android:gravity="right" android:layout_width="0dip" 
     android:layout_weight="2"/> 
</LinearLayout> 
+0

工作正常!但为什么不能解决我的问题?有什么不对吗?或者它只是Android/LinearLayout的行为?我在另一个项目中使用了一个类似的构造,它运行良好。 – kadir

+0

http://stackoverflow.com/questions/5986793/in-android-layouts-what-is-the-effect-meaning-of-layout-height-0dip – blessenm

0

尝试这样

<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
<TextView android:id="@+id/feedfragmentTitle" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="left"/> 
<TextView android:id="@+id/feedfragmentDate" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_width="wrap_content" /> 
</LinearLayout>