繁体   English   中英

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

[英]Android: LinearView with fixed sized widgets left and right and a flexible one in the middle

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

我有一个水平的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>

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

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

看看这个答案Android:LinearView左右固定大小的小部件和中间的灵活小部件你可能只需要将中间元素的layout_width设置为0px,使其不使用内容获得的宽度,而是通过重量。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM