简体   繁体   English

Android布局如何使用动态宽度水平对齐textView

[英]Android layout how to horizontal align textView with dynamical width

Having 4 views A (TextView), B(icon), C(TextView), D (TextView) horizontally aligned next to each other.有 4 个视图 A (TextView), B(icon), C(TextView), D (TextView) 彼此水平对齐。

B and D have fixed width. B 和 D 具有固定的宽度。 The text will be filled in the onBindView().文本将填充在 onBindView() 中。

Their parent's width could be calculated based on the screen orientation.可以根据屏幕方向计算其父级的宽度。

It requires to show A as much as possible, and the C will consume the rest or up to its content width (if not enough space then show ellipsis).它需要尽可能多地显示 A,而 C 将消耗其余部分或直至其内容宽度(如果空间不足,则显示省略号)。

And all A, B, C, D should left align the right edge of the previous one.并且所有 A、B、C、D 都应左对齐前一个的右边缘。

There are a few cases but these four cover most of them.有几个案例,但这四个涵盖了大部分。

1. Parent width is enough for all

|-|AAAA|B|CCCCCCCC|DDD|--------------|-|

2. C is too large and have to show ellipses


|-|AAAAA|B|CCCCCCCCCCCCC...|DDD|-|

  3. A is large and have to show ellipse for C to be able to show

|-|AAAAAAA..........|B|CCCCCCCC|DDD|-|



4. the parent width is not enough for showing A and C completely, so A and C both show ellipses


|-|AAAAAAAAAAAAAA...|B|CCCCC...|DDD|-|

so the rule is to show A as much as possible, C shows either in full or with ellipses, in case with showing C's minWidth and still cannot show the A in full then show A with ellipsis.所以规则是尽可能多地显示 A,C 显示完整或带有省略号,以防显示 C 的 minWidth 并且仍然不能完整显示 A,然后显示带有省略号的 A。

Tried LinearLayout, RelativeLayout and ConstraintLayout and cant get the desired result.尝试了 LinearLayout、RelativeLayout 和 ConstraintLayout 并不能得到想要的结果。

this has two maxWidth for A and C, the problem is if either A or C is too small the other one may still show ellipsis without using all available space.这对于 A 和 C 有两个 maxWidth,问题是如果 A 或 C 太小,另一个可能仍然显示省略号而不使用所有可用空间。

   <LinearLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:orientation="horizontal"
        >


        <TextView
            android:id="@+id/a_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxWidth="130dp"
            android:maxLines="1"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="ee" />

        <ImageView
            android:id="@+id/b_checked"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_marginStart="3dp"
            android:layout_marginEnd="8dp"
            android:baselineAlignBottom="true"
            app:srcCompat="@drawable/checked" />

        <TextView
            android:id="@+id/c_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:ellipsize="end"
            android:maxWidth="150dp"
            android:maxLines="1"
            tools:text="aasdasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfa" />

        <TextView
            android:id="@+id/d_time"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_gravity="left"
            tools:text="30 Nov 18"
            />

    </LinearLayout>

If do it in a derived view and override the onMeasure(), the calculation is not straight forward and the A, C width may not immediately available.如果在派生视图中执行此操作并覆盖 onMeasure(),则计算不是直接进行的,并且 A、C 宽度可能不会立即可用。

How to do it just in layout xml, or is it possible?如何仅在布局 xml 中做到这一点,或者有可能吗?

I'm not sure if this meets your requirements, but this is my attempt using ConstraintLayout我不确定这是否符合您的要求,但这是我使用ConstraintLayout尝试

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#eee"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="something very very very very very long"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/two"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fixed"
        app:layout_constraintBaseline_toBaselineOf="@id/one"
        app:layout_constraintLeft_toRightOf="@id/one"
        app:layout_constraintRight_toLeftOf="@id/three"/>

    <TextView
        android:id="@+id/three"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#eee"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="this is pretty short"
        app:layout_constraintBaseline_toBaselineOf="@id/one"
        app:layout_constraintLeft_toRightOf="@id/two"
        app:layout_constraintRight_toLeftOf="@id/four"
        app:layout_constraintWidth_default="wrap"/>

    <TextView
        android:id="@+id/four"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fixed"
        app:layout_constraintBaseline_toBaselineOf="@id/one"
        app:layout_constraintLeft_toRightOf="@id/three"
        app:layout_constraintRight_toRightOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

The important parts here are that the first "stretchable" view uses wrap_content while the second uses 0dp plus app:layout_constraintWidth_default="wrap" .这里的重要部分是第一个“可拉伸”视图使用wrap_content而第二个使用0dp加上app:layout_constraintWidth_default="wrap" This will give the space to the first view at a higher priority than the second view, but will allow both to grow and shrink.这将为第一个视图提供比第二个视图更高优先级的空间,但将允许两者增长和缩小。

The only problem I've found is that, when the first view's text is very long, it can push the other views on top of each other.我发现的唯一问题是,当第一个视图的文本长时,它可以将其他视图推到彼此的顶部。 This could potentially be solved by adding a maximum width to the first view:这可以通过向第一个视图添加最大宽度来解决:

app:layout_constraintWidth_max="256dp"

Some screenshots:一些截图:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

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

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