【问题标题】:Android wrap_content too wide when centred text of TextView is long当 TextView 的居中文本很长时,Android wrap_content 太宽
【发布时间】:2017-06-22 08:45:57
【问题描述】:

您可以使用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 的宽度与父级的宽度一样宽,当文本换行时,不计算 actual 文本块的宽度。

那么,我该如何实现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>

【问题讨论】:

  • IMO,实现这一点的最简单方法是将所有带有“left”标签的文本视图放在具有“center”标签和“wrap_content”宽度的垂直线性布局中。
  • 好的,我去试试。
  • @Dexter 嗯,不,不起作用。我的 TextViews 实际上已经在 LinearLayout 中了。你会得到的是(A)。
  • 简单地使用左或/和右填充
  • @pskink:我不能使用固定填充,因为我不知道文本的长度,所以填充可能会浪费可用空间。

标签: android android-layout text-alignment layout-gravity


【解决方案1】:

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

<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>

【讨论】:

  • 你改变了什么?
  • 啊,我明白了。双重嵌套。不是很优雅,但似乎工作!谢谢!
  • 嗯,恐怕不行。文本不再居中。 primary_form 和 secondary_form 也应该独立运行。
  • 玩了一会儿后,我的结论是您的解决方案比我真正想要实现的要好。 :)
  • @digorydoo :如果您不想要嵌套线性布局。我已经更新了答案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-18
  • 1970-01-01
  • 2023-03-16
  • 2011-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多