【发布时间】: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