Android wrap_content 当 TextView 的居中文本很长时太宽

Android wrap_content too wide when centred text of TextView is long

您可以使用 layout_gravitygravity 进行文本居中。随着 layout_gravity:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:breakStrategy="balanced"
/>

短文本将显示为:

[          Hello          ]

长距离会是:(A)

[ One lemming looking     ]
[ up.                     ]

所以你可以添加:

    android:gravity="center"

你会得到:(B)

[   One lemming looking   ]
[           up.           ]

不过,我想实现这个:(C)

[   One lemming looking   ]
[   up.                   ]

A 看起来不太好,因为文本没有居中显示。如果布局中有更多居中元素,则尤其如此。

B不太好看,因为很短的单词"up"被放到了中间。即使文本更长,它看起来也不好看,因为文本有两个锯齿状边缘(左 + 右)而不是只有一个(向右)。

CA 相同,但正确居中。 A 看起来不居中的原因是因为 wrap_content 假定文本在另一行换行时 TextView 的宽度与父视图的宽度一样宽,并且不计算文本块的实际宽度。

那么,如何实现C呢?可能吗?

完整布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
/>
<TextView
    android:id="@+id/primary_form"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="start"
    android:textAppearance="@style/TextAppearance.AppCompat.Headline"
    android:textColor="@color/text_primary_on_lt"
    android:breakStrategy="balanced"
    tools:text="Primary form adfahdkahdfa "
/>
<TextView
    android:id="@+id/secondary_form"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="8dp"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    android:textColor="@color/text_secondary_on_lt"
    android:textStyle="italic"
    android:typeface="serif"
    android:breakStrategy="balanced"
    tools:text="Secondary form"
/>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
/>
</LinearLayout>

您可以通过多种方式解决这个问题,但我将向您展示两种方式,首先是使用老式线性布局

<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="match_parent"
    android:layout_marginLeft="32dp"
    android:layout_marginRight="32dp"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/primary_form"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:breakStrategy="balanced"
            android:gravity="start"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            tools:text="Primary form adfahdkahdfa " />

        <TextView
            android:id="@+id/secondary_form"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:breakStrategy="balanced"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textStyle="italic"
            android:typeface="serif"
            tools:text="Secondary form" />
    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

现在有最新的约束布局。这是更健壮和推荐的方式。假设您希望您的主要文本位于其正下方的中间和次要文本中,

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="32dp"
    android:layout_marginRight="32dp"
    android:orientation="vertical">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:breakStrategy="balanced"
        android:gravity="start"
        android:textAlignment="center"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        app:layout_constraintBottom_toTopOf="@+id/guideline16"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:text="Primary form adfahdkahdfa " />

    <TextView
        android:id="@+id/secondary_form"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:breakStrategy="balanced"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textStyle="italic"
        android:typeface="serif"
        app:layout_constraintLeft_toLeftOf="@+id/textView"
        app:layout_constraintTop_toTopOf="@+id/guideline16"
        tools:text="Secondary form" />


    <android.support.constraint.Guideline
        android:id="@+id/guideline16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.50793654"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="288dp" />

    <FrameLayout
        android:id="@+id/layout1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/secondary_form" />

    <FrameLayout
        android:id="@+id/layout2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>