2010-11-16 87 views
3

似乎是一个简单的问题,但我无法弄清楚如何去做。Android:带有固定大小小部件的LinearView,左侧和右侧,以及中间的小部件灵活

我有左侧固定大小的东西水平LinearView,然后是一些文本,应采取如密歇根州的空间成为可能,但应该是另一种固定大小的物体的权利。现在

,我的问题现在是:当在中间的文本太长,在合适的对象被推出窗外。当文字太短时,它不会坐在窗口的右边,而是直接放在文字的旁边。

我该怎么办?到目前为止我的代码(如你所见,我试图用LayoutWeight,这并没有帮助...):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:id="@+id/important" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="10dp" 
     android:background="#000000" 
     android:layout_weight="1"/> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"> 
     <TextView 
      android:id="@+id/name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingTop="5dp" 
      android:paddingRight="5dp" 
      android:paddingLeft="0dp" 
      android:textColor="#333333" 
      android:paddingBottom="0dp" 
      android:textSize="14sp" 
      android:scrollHorizontally="true"/> 
     <TextView 
      android:id="@+id/description" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#666666" 
      android:paddingTop="3dp" 
      android:paddingRight="5dp" 
      android:paddingLeft="10dp" 
      android:paddingBottom="5dp" 
      android:textSize="12sp" /> 
    </LinearLayout> 

    <ImageView android:id="@+id/lastpage" 
     android:src="@drawable/lastpage" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_weight="200" 
     android:scaleType="center" 
     android:layout_gravity="right"/> 
</LinearLayout> 

回答

6

看着这个答案Android: LinearView with fixed sized widgets left and right and a flexible one in the middle,你也许只需要设置你的中段layout_width元素设置为0px以使其不使用内容获得的宽度,而是通过重量来决定它。

希望这会有所帮助。

+0

工作!非常感谢! – janoliver 2010-11-16 21:20:38

+0

很高兴能帮到您:) – sunadorer 2010-11-16 21:21:53

+0

链接转发回到这个页面 – inVINCEable 2015-01-09 15:27:58

7

对于两个小工具(LinearLayout的第一个和第二个小孩),您的android:layout_weight="1"与您的业务规则不匹配。

就个人而言,我会在这里使用一个RelativeLayout。在中间使用android:layout_alignParentLeft="true"上的第一个孩子,android:layout_alignParentRight="true"的第三个孩子,和android:toLeftOfandroid:toRightOf,外固定子女之间固定好。这更明确地实现您所需的业务规则。

+0

亲爱的CommonsWare,感谢您的评论,实际上我试着切换到RelativeLayout,但是sunadorer的解决方案能够正常工作,现在我将保持原样。但是,当我找到时间时,我会尝试再次切换,因为这是Android文档中推荐的。但感谢您的帮助! – janoliver 2010-11-16 21:19:57

相关问题