android - 居中左对齐到最长的TextView TextViews 在带有换行内容的LinearLayout 中不起作用
问题描述
我想创建一个布局,其中有多行不同长度的文本。所有文本都居中。居中的最长文本的起点将作为基础。然后,其他较短的文本将不再居中,而是向左对齐到最长文本的起点。
为了在 ConstraintLayout 中实现这一点,我创建了一个居中且宽度为wrap_content
. 然后,在 LinearLayout 内部,我也有多个TextView
带有 width 的 s wrap_content
。我希望 LinearLayout 自行扩展(宽度),直到 ConstraintLayout 的宽度(具有设备的宽度),以确保其子级TextView
不会换行,除非文本长于屏幕宽度(其中,在我的特殊情况下,是不可能的)。Android Studio 编辑器的预览显示这正如我预期的那样发生。但是,在我的设备上运行并Tools > Layout Inspector
在 Android Studio 上使用时,即使剩余空间很大,LinearLayout 也不会自行扩展宽度。
这是我用于布局的 XML 代码。
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="user"
type="com.mobile.githubuser.model.GithubUser" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/act_details_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:background="?attr/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="@string/act_user_details_toolbar_title"
app:titleTextAppearance="@style/TextAppearance.Toolbar" />
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/shapeableImageView"
style="@style/Widget.GithubUser.ShapeableImageView.Circular"
android:layout_width="@dimen/user_details_avatar_size"
android:layout_height="@dimen/user_details_avatar_size"
android:layout_marginTop="@dimen/keyline_5"
android:src="@{user.avatar}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/act_details_toolbar" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_4"
android:layout_marginTop="@dimen/keyline_4"
android:layout_marginEnd="@dimen/keyline_4"
android:text="@{user.name}"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceHeadline2"
android:textColor="?attr/colorOnBackground"
app:layout_constraintTop_toBottomOf="@+id/shapeableImageView"
tools:layout_editor_absoluteX="16dp"
tools:text="April Ludgate" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_4"
android:layout_marginTop="@dimen/keyline_2"
android:layout_marginEnd="@dimen/keyline_4"
android:text="@{`@` + user.username}"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceSubtitle2"
android:textColor="?attr/colorOnBackground"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView"
tools:text="\@april_ludgate" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_4"
android:layout_marginTop="@dimen/keyline_5"
android:layout_marginEnd="@dimen/keyline_4"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView2">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/drawable_company"
android:drawablePadding="@dimen/keyline_2"
android:text="@{`Company\t\t: ` + user.company}"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView2"
tools:text="\Company\t\t: Google, Inc." />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/keyline_0"
android:drawableStart="@drawable/drawable_location"
android:drawablePadding="@dimen/keyline_2"
android:text="@{`Location\t\t\t: ` + user.location}"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView3"
tools:text="Location\t\t\t: Pittsburgh, PA, USA" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/keyline_0"
android:drawableStart="@drawable/drawable_repository"
android:drawablePadding="@dimen/keyline_2"
android:text="@{`Repository\t: ` + user.repository}"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView4"
tools:text="Repository\t: 2" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/keyline_0"
android:drawableStart="@drawable/drawable_follower"
android:drawablePadding="@dimen/keyline_2"
android:text="@{`Followers\t\t: `}"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
addNumberToText="@{user.followers}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView5"
tools:text="Followers\t\t: 1029" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/keyline_0"
android:drawableStart="@drawable/drawable_following"
android:drawablePadding="@dimen/keyline_2"
android:text="@{`Following\t\t: `}"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
addNumberToText="@{user.following}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView6"
tools:text="Following\t\t: 101" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
这是我的编辑器预览显示的(我想要的)。
这是显示问题的工具检查器。
正如您在第二张图片中看到的那样,TextView
当LinearLayout
. 我希望LinearLayout
占用尽可能多的空间,但尽可能少的空间仍然TextView
在一行中显示所有内容。分别使用android:maxLines=1
和android:singleLine=true
使文本被截断和省略;我不想要这个。是什么导致了这个问题?如果以这种方式实现我想要的东西是不可能的,我怎样才能实现我所描述的(不使用固定值,例如边距和指南来对齐TextView
s)?
与问题无关:为什么MaterialTextView
我的 Android Studio eidtor 预览中无法正确显示?
解决方案
正如@Pawel Laskowski 的评论中提到的,是制表符 ( \t
) 导致布局出现问题。只需擦除制表符并用几个空格替换它们即可。但是,由于我使用的字体不是等宽字体,所以冒号部分没有完美对齐。
另一种解决方案是将每行一个 TextView 分成两个 TextView。这也对我有利,因为现在,我可以提供多种语言的字符串文字(例如Company
,等),并将动态生成的内容(来自 data-binding )添加到整个显示的文本中。然后,在所有分离之后,将 TextViews 放置在 ConstraintLayout 中,替换原来的 LinearLayout。现在,我们的结构如下(对于有问题的部分)。Location
user
ConstraintLayout
|__ TextView (first part for row 1)
|__ TextView (second part for row 1)
|__ ...
|__ TextView (first part for row n)
|__ TextView (second part for row n)
然后,创建一个障碍,其方向end
引用每行的所有第一部分。将每行的所有第二部分相对于障碍放置并添加一些marginStart
以在第一部分和第二部分之间创建空间。第二部分包含冒号和动态生成的内容。
这是最终的 XML。
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="user"
type="com.mobile.githubuser.model.GithubUser" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/act_details_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:background="?attr/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="@string/act_user_details_toolbar_title"
app:titleTextAppearance="@style/TextAppearance.Toolbar" />
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/shapeableImageView"
style="@style/Widget.GithubUser.ShapeableImageView.Circular"
android:layout_width="@dimen/user_details_avatar_size"
android:layout_height="@dimen/user_details_avatar_size"
android:layout_marginTop="@dimen/keyline_5"
android:src="@{user.avatar}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/act_details_toolbar" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_4"
android:layout_marginTop="@dimen/keyline_4"
android:layout_marginEnd="@dimen/keyline_4"
android:text="@{user.name}"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceHeadline2"
android:textColor="?attr/colorOnBackground"
app:layout_constraintTop_toBottomOf="@+id/shapeableImageView"
tools:layout_editor_absoluteX="16dp"
tools:text="April Ludgate" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_4"
android:layout_marginTop="@dimen/keyline_2"
android:layout_marginEnd="@dimen/keyline_4"
android:text="@{`@` + user.username}"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceSubtitle2"
android:textColor="?attr/colorOnBackground"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView"
tools:text="\@april_ludgate" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_4"
android:layout_marginTop="@dimen/keyline_5"
android:layout_marginEnd="@dimen/keyline_4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView2">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/drawable_company"
android:drawablePadding="@dimen/keyline_2"
android:text="@string/company_label"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/keyline_0"
android:drawableStart="@drawable/drawable_location"
android:drawablePadding="@dimen/keyline_2"
android:text="@string/location_label"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView3" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/keyline_0"
android:drawableStart="@drawable/drawable_repository"
android:drawablePadding="@dimen/keyline_2"
android:text="@string/repository_label"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView4" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/keyline_0"
android:drawableStart="@drawable/drawable_follower"
android:drawablePadding="@dimen/keyline_2"
android:text="@string/followers_label"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView5" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/keyline_0"
android:drawableStart="@drawable/drawable_following"
android:drawablePadding="@dimen/keyline_2"
android:text="@string/following_label"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialTextView6" />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="materialTextView3,materialTextView4,materialTextView5,materialTextView6,materialTextView7"/>
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_2"
android:text="@{`: ` + user.company}"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintBottom_toBottomOf="@+id/materialTextView3"
app:layout_constraintStart_toEndOf="@id/barrier2"
app:layout_constraintTop_toTopOf="@+id/materialTextView3"
tools:text=": Google, Inc." />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_2"
android:text="@{`: ` + user.location}"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintBottom_toBottomOf="@+id/materialTextView4"
app:layout_constraintStart_toEndOf="@id/barrier2"
app:layout_constraintTop_toTopOf="@+id/materialTextView4"
tools:text=": Pittsburgh, PA, U.S.A." />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView10"
formatNumberToTextWithColon="@{user.repository}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_2"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintBottom_toBottomOf="@+id/materialTextView5"
app:layout_constraintStart_toEndOf="@id/barrier2"
app:layout_constraintTop_toTopOf="@+id/materialTextView5"
tools:text=": 1,234" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/materialTextView11"
formatNumberToTextWithColon="@{user.followers}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_2"
android:layout_marginBottom="2dp"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintBottom_toBottomOf="@+id/materialTextView6"
app:layout_constraintStart_toEndOf="@id/barrier2"
app:layout_constraintTop_toTopOf="@+id/materialTextView6"
tools:text=": 304" />
<com.google.android.material.textview.MaterialTextView
formatNumberToTextWithColon="@{user.following}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_2"
android:textAppearance="?attr/textAppearanceBody1"
android:textColor="?attr/colorOnBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/barrier2"
app:layout_constraintTop_toTopOf="@+id/materialTextView7"
tools:text=": 1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
推荐阅读
- sql-server - 让 XML 输入 SQL Server 表
- typo3 - EXT:Form TYPO3 表单装订器设置不起作用
- prolog - 使用 read/1 时出现意外的 end_of_file
- python - Django 使用 onetoone 扩展用户模型,但在应用程序中有几个 model.py 文件
- text - 什么是 unicode 字符,甚至是 ⏭ 的字形,但只有一个三角形
- python-3.x - 无法弄清楚 Codewars Growth of a Population kata 中的错误
- c# - 在 MSTest 中,如何指定某些测试方法不能相互并行运行?
- jquery - 更改其类后删除此元素
- windows - 在 Windows CMD 中提取给定的祖先路径形成更深的路径
- java - 将 Univocity 解析器与 Spring Batch 一起使用