简体   繁体   English

具有可调整包装的TextView的LinearLayout

[英]LinearLayout with adaptable wrapping of TextView

I'm struggling to refine a ListView row item which has three specific width columns. 我正在努力完善具有三个特定宽度列的ListView行项目。

Column A has a Button and vertical divider View 列A具有一个Button和垂直分隔线View

Column B has a TextView and may or may not have an ImageView showing next to it depending on circumstances B列具有TextView ,根据情况可能不显示在其旁边的ImageView

Column C has RelativeLayout that can show nothing, or a TextView , CheckBox or ImageView depending on circumstances C列具有不能显示任何内容的RelativeLayout ,或者视情况而定的TextViewCheckBoxImageView

See example image. 参见示例图片。 The Herring Gull row doesn't need an icon. 鲱鸥行不需要图标。 The Herring x Lesser Black-backed Gull (hybrid) needs a red icon, similar to the other species above and below it. 鲱鱼x小黑背海鸥(杂交)需要一个红色图标,类似于其上方和下方的其他物种。 However, the longer species name is pushing the icon out of view when it wraps onto two lines. 但是,物种名称越长,将其包裹在两行上时,图标就越无法看到。

在此处输入图片说明

<?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>

I have tried replacing the TextView & ImageView combination with a single TextView populated with a SpannableStringBuilder , but that positioned the icon at the end of the wrapped text (see example with Green Woodpecker), when I'd like it positioned to the right of the text. 我尝试用填充有SpannableStringBuilder的单个TextView替换TextViewImageView组合,但是当我希望将图标放置在包装文本的末尾时(请参见绿色啄木鸟的示例)文本。

在此处输入图片说明

My desired behaviour is for the ImageView icon to always be right next to the TextView , and when the TextView is long, the ImageView becomes dominant within Column B and the TextView wraps it's text into a slightly smaller space. 我想要的行为是使ImageView图标始终位于TextView旁边,当TextView长时, ImageView在B列中占据主导地位,而TextView则将其文本包装到稍小的空间中。

Is this possible please? 请问这可能吗?

Try removing the ImageView with the icon and add an icon directly to the TextView if you want to show it with the following line of code: 如果要使用以下代码行显示它,请尝试删除带有该图标的ImageView并将一个图标直接添加到TextView

textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.your_id, 0);

Hope this helps 希望这可以帮助

it is hard to understand from your question completely , but what i've decoded is you require to place textview and imageview side by side that can only be possible if you hardcode textview width and give textview height as match_paren t , something like below: 很难从您的问题中完全理解,但是我已经解码了,您需要并排放置textviewimageview ,这只有在您将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: code is not tested it is just a general idea PS:未经测试的代码只是一个总体思路

You have to declare a maxWidth for your textView android:id="@+id/textview_species_name" in order to make it work properly. 为了使其正常工作,您必须为textView android:id="@+id/textview_species_name"声明一个maxWidth。 Keep in mind that it has to be dynamic based on device's screen width 请记住,它必须根据设备的屏幕宽度是动态的

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

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