Android:LinearView左右固定大小的小部件,中间是灵活的小部件

时间:2010-11-16 20:55:56

标签: android android-linearlayout

似乎是一个简单的问题,但我无法弄清楚如何去做。

我有一个水平的LinearView,左边有一些固定大小的东西,然后有一些文本应该尽可能地作为mich空间,但右边应该是另一个固定大小的对象。

现在,我的问题就在于:当中间的文本太长时,右边的对象会被推出窗口。当文本太短时,它不会位于窗口的右边界,而是直接位于文本旁边。

我该怎么办?到目前为止我的代码(正如你所看到的,我试图使用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>

2 个答案:

答案 0 :(得分:7)

您在两个小部件(android:layout_weight="1"的第一个和第二个子级)上有LinearLayout,这与您的业务规则不符。

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

答案 1 :(得分:6)

查看此答案Android: LinearView with fixed sized widgets left and right and a flexible one in the middle,您可能只需要将中间元素的layout_width设置为0px,以使其不使用内容所获得的宽度,而是通过权重来决定它。

希望这有帮助。