[英]LinearLayout with adaptable wrapping of TextView
我正在努力完善具有三个特定宽度列的ListView
行项目。
列A具有一个Button
和垂直分隔线View
B列具有TextView
,根据情况可能不显示在其旁边的ImageView
C列具有不能显示任何内容的RelativeLayout
,或者视情况而定的TextView
, CheckBox
或ImageView
参见示例图片。 鲱鸥行不需要图标。 鲱鱼x小黑背海鸥(杂交)需要一个红色图标,类似于其上方和下方的其他物种。 但是,物种名称越长,将其包裹在两行上时,图标就越无法看到。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/species_row_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal">
<Button
android:id="@+id/button_count"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.15"
android:background="@null"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:text="+"
android:textColor="@color/light_gray"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:background="@color/bpWhite"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:layout_marginRight="3dp"
android:layout_weight="0.70">
<TextView
android:id="@+id/textview_species_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:minHeight="44dp"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:text="Species name"
android:textColor="#000"
android:textSize="15sp"/>
<ImageView
android:id="@+id/speciesBinIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingStart="5dp"
android:scaleType="fitXY"
android:src="@drawable/ic_uncommon"
android:visibility="gone"/>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:gravity="center_vertical">
<TextView
android:id="@+id/textview_rare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="4dp"
android:background="@drawable/rare_border"
android:gravity="center"
android:text="@string/single_character_for_rare"
android:textColor="@color/rareBackground"
android:textStyle="bold"
android:visibility="gone"/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/rareBackground"
android:gravity="center"
android:visibility="gone"/>
<ImageView
android:id="@+id/high_count_warning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginStart="6dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:scaleType="fitXY"
android:src="@drawable/ic_high_count"
android:visibility="gone"/>
</RelativeLayout>
我尝试用填充有SpannableStringBuilder
的单个TextView
替换TextView
和ImageView
组合,但是当我希望将图标放置在包装文本的末尾时(请参见绿色啄木鸟的示例)文本。
我想要的行为是使ImageView
图标始终位于TextView
旁边,当TextView
长时, ImageView
在B列中占据主导地位,而TextView
则将其文本包装到稍小的空间中。
请问这可能吗?
如果要使用以下代码行显示它,请尝试删除带有该图标的ImageView
并将一个图标直接添加到TextView
:
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.your_id, 0);
希望这可以帮助
很难从您的问题中完全理解,但是我已经解码了,您需要并排放置textview和imageview ,这只有在您将textview宽度硬编码并将textview高度设置为match_paren t时才能实现,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/species_row_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal">
<Button
android:id="@+id/button_count"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.15"
android:background="@null"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:text="+"
android:textColor="@color/light_gray"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:background="@color/bpWhite"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="3dp"
android:layout_marginRight="3dp"
android:layout_weight="0.90">
<TextView
android:id="@+id/textview_species_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:toStartOf="@+id/speciesBinIcon"
android:layout_gravity="center_vertical"
android:minHeight="44dp"
android:paddingBottom="3dp"
android:paddingTop="3dp"
android:text="Species name"
android:textColor="#000"
android:textSize="15sp"/>
<ImageView
android:id="@+id/speciesBinIcon"
android:alignParentEnd="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingStart="5dp"
android:scaleType="fitXY"
android:src="@drawable/ic_uncommon"
android:visibility="gone"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:gravity="center_vertical">
<TextView
android:id="@+id/textview_rare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="4dp"
android:background="@drawable/rare_border"
android:gravity="center"
android:text="@string/single_character_for_rare"
android:textColor="@color/rareBackground"
android:textStyle="bold"
android:visibility="gone"/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/rareBackground"
android:gravity="center"
android:visibility="gone"/>
<ImageView
android:id="@+id/high_count_warning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginStart="6dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:scaleType="fitXY"
android:src="@drawable/ic_high_count"
android:visibility="gone"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
PS:未经测试的代码只是一个总体思路
为了使其正常工作,您必须为textView android:id="@+id/textview_species_name"
声明一个maxWidth。 请记住,它必须根据设备的屏幕宽度是动态的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.