【发布时间】:2014-12-12 04:23:19
【问题描述】:
我希望在我的应用中进行以下布局处理:
如您所见,文本可以尽可能宽,而不会将出现在其右侧的其他视图推离屏幕,并根据需要进行省略。色样永远不会超过 3 个,并且不得少于 1 个,并且所有色样和圆圈中的 x 必须始终可见。 当文本很短时,它的行为应该与最后 2 行(蕨类植物)中看到的一样。
我尝试将文本和图像放在LinearLayout 中,但是当文本太长时,图像不可见(或不完全可见)。我认为有某种方法可以表明图像应始终占用所需的空间,TextView 占用其余空间或所需空间,但我似乎无法弄清楚如何。我不知道RelativeLayout 是否会更好地解决这个问题,或者TableLayout/TableRow 或GridLayout,尽管我读过的任何内容似乎都没有涵盖这种情况。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/plant_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:maxLines="1"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginRight="3dp"/>
<LinearLayout
android:id="@+id/color_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_remove_plant"
android:visibility="invisible"/>
</LinearLayout>
<LinearLayout
android:id="@+id/color_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_remove_plant"
android:visibility="invisible"/>
</LinearLayout>
<LinearLayout
android:id="@+id/color_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_remove_plant"
android:visibility="invisible"/>
</LinearLayout>
<ImageView
android:id="@+id/remove_plant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_remove_plant"
android:layout_marginLeft="3dp"/>
</LinearLayout>
【问题讨论】:
-
@TimCastelijns 我明白了。我忽略了这一点。
-
您可以使用RelativeLayout,从右侧开始,随着您的视图向左移动。首先调用 layout_alignParentRight="true",然后为以下每个视图调用 layout_toLeftOf="@id/id_of_view_to_the_right"。对于最后一个视图,使宽度 match_parent 并将布局设置为最终颜色视图的左侧。
-
@zgc7009 你将如何处理最右边的视图未锚定到右边缘的底部 2 行?
-
@TimCastelijns 误读了这个问题,认为问题在于最后两个应该看起来像其余的(除了文本之外的所有内容)。
-
这是 ListView 还是固定布局?