代码之家  ›  专栏  ›  技术社区  ›  dommer

我如何阻止Android TextView中有很多文本挤到右边?

  •  1
  • dommer  · 技术社区  · 7 年前

    我试图获得两个并排且居中的文本视图——允许它们随着数据的增长而增长。当文本为 大,我在省略它。

    目前,当我在两个框中有以下文字时

    • 一个大的长单词列表,将另一个文本视图推出
    • Foobar公司

    两者都受到挤压。

    在这种情况下,我希望“Foobar”被完全显示,而较长的TextView会留出一些空间——例如

    ...the other TextView out Foobar

    我现在得到的是

    ...that pushes the other TextView out ...ar

    我的尝试如下。

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:maxLines="1"
            android:ellipsize="start"
            android:textSize="20dp"
            android:text="A big long list of words that pushes the other TextView out" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:maxLines="1"
            android:ellipsize="start"
            android:textSize="20dp"
            android:text="Foobar" />
    </LinearLayout>
    

    我还尝试了一个相对输出(见下文)——带有minWidth——但这只是将最右边的TextView完全从父视图中挤出!

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:maxLines="1"
            android:ellipsize="start"
            android:textSize="20dp"
            android:text="A big long list of words that pushes the other TextView out" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/textView1"
            android:minWidth="100dp"
            android:maxLines="1"
            android:ellipsize="start"
            android:textSize="20dp"
            android:text="Foobar" />
    </RelativeLayout>
    

    有人知道怎么做吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Cheticamp    7 年前

    看看谷歌的FlexboxLayout。我认为它可能具有您需要的能力来流动您的 TextView here 还有一个教程 here .

    FlexboxLayout是一个库项目,它为Android带来了类似CSS灵活框布局模块的功能。

    我不确定 FlexboxLayout 是解决方案。这是另一个使用 TableLayout TableRow

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="0">
    
        <!-- TableLayout is needed to specify shrinkColumns. Ignore "Useless parent" error. -->
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:ignore="UselessParent">
    
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:ellipsize="start"
                android:maxLines="1"
                android:text="A big long list of words that pushes the other outwords that pushes the other TextView out"
                android:textSize="20sp" />
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:layout_weight="1"
                android:maxLines="1"
                android:text="Foobar"
                android:textSize="20sp" />
    
        </TableRow>
    </TableLayout>