【问题标题】:TextView does not wrap content when the text wraps in second line当文本在第二行换行时,TextView 不换行内容
【发布时间】:2019-06-13 18:16:51
【问题描述】:

下面为什么TextView在文本换行到第二行时有多余的空间(在下图中用黄色框突出显示)?我怎样才能删除它?

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        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:layout_gravity="center"
        android:layout_weight="2"
        android:background="@color/green"
>

    <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_weight="1"
        >

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:padding="0dp"
                android:layout_gravity="center"
                android:text="THIS IS FOOOOOOOOOOOOOOO"
                android:background="@color/red"
        />
    </LinearLayout>

    <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_weight="1"

    >

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:padding="0dp"
                android:layout_gravity="center"
                android:text="THIS IS FOOOOOOOOOOOOOOO"
                android:background="@color/red"
        />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 您无能为力。 textview 首先尝试尽可能扩大以适应您的文本,然后当这还不够时,它会自动换行。但它不会重新调整自己的大小以适应换行的文本。
  • @BenP.:真的吗?有没有办法以编程方式做到这一点?这样做的问题是,在这种情况下,由于 textview 额外空间,布局不再居中。从技术上讲,它是居中的,但在视觉上并不明显
  • 我不知道有什么方法可以在不重新实现自己的文本视图的情况下实现你想要的。您可以将gravity="center_horizontal" 应用于您的文本视图,但这样您就不会在多行中出现左对齐文本。
  • 尝试为文本视图设置 android:layout_width="0dp" 和 android:layout_weight="1"
  • @Jim Ben P 是对的,除了调整文本大小,您无能为力

标签: android textview android-linearlayout android-textview-autosize


【解决方案1】:

可以尝试一些蛮力。首先将文本拆分为行。循环它们,测量宽度,如果不超过 textView 的宽度,则附加到字符串生成器,如果大于 textView 的宽度,逐个字符测量,如果不超过,则附加到字符串生成器,否则将“\n”附加到字符串生成器。通过此构建器获取字符串并将其设置为 textView。

【讨论】:

    【解决方案2】:

    真正的原因是您将 android:layout_weight 属性赋予了 LinearLayout。根据布局分离发生删除 android:layout_weight 并检查如下

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
            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:layout_gravity="center"
            android:background="@color/green">
    
        <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:gravity="center">
    
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:padding="0dp"
                    android:layout_gravity="center"
                    android:text="THIS IS FOOOOOOOOOOOOOOO"
                    android:background="@color/red"
            />
        </LinearLayout>
    
        <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:gravity="center">
    
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:padding="0dp"
                    android:layout_gravity="center"
                    android:text="THIS IS FOOOOOOOOOOOOOOO"
                    android:background="@color/red"
            />
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多