Android 居中左对齐到最长文本视图文本视图不使用换行内容的线性布局
我想创建一个布局,其中有多行不同长度的文本。所有文本都居中。居中最长文本的起点将作为基础。然后,其他较短的文本将不再居中,而是左对齐到最长文本的起点 为了在ConstraintLayout中实现这一点,我创建了一个居中的线性布局,宽度为Android 居中左对齐到最长文本视图文本视图不使用换行内容的线性布局,android,android-layout,textview,android-linearlayout,android-constraintlayout,Android,Android Layout,Textview,Android Linearlayout,Android Constraintlayout,我想创建一个布局,其中有多行不同长度的文本。所有文本都居中。居中最长文本的起点将作为基础。然后,其他较短的文本将不再居中,而是左对齐到最长文本的起点 为了在ConstraintLayout中实现这一点,我创建了一个居中的线性布局,宽度为wrap\u content。然后,在LinearLayout内部,我有多个TextViews,宽度也为wrap\u content。我希望LinearLayout将自身(宽度)扩展到ConstraintLayout的宽度(具有设备的宽度),以确保其子项TextV
wrap\u content
。然后,在LinearLayout内部,我有多个TextView
s,宽度也为wrap\u content
。我希望LinearLayout将自身(宽度)扩展到ConstraintLayout的宽度(具有设备的宽度),以确保其子项TextView
s不会换行,除非文本长于屏幕宽度(在我的特殊情况下,这是不可能的)。androidstudio编辑器的预览显示,这正如我预期的那样发生。但是,在我的设备上运行并在Android Studio上使用Tools>Layout Inspector
时,即使剩余空间很大,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)
与问题无关:为什么我的材质文本视图在我的Android Studio eidtor预览中不能正确显示?如中所述,是制表符(\t
)导致布局出现问题。只需删除制表符并用几个空格替换即可。但是,由于我使用的字体不是等距的,冒号部分没有完全对齐
另一种解决方案是将每行一个文本视图分成两个文本视图。这也对我有利,因为现在,我可以以多种语言提供字符串文字(例如,公司
,位置
,等等),并将动态生成的内容(从数据绑定用户
)添加到整体显示的文本中。然后,在所有分隔之后,将文本视图放置在ConstraintLayout中,替换原始线性布局。现在,我们的结构如下(对于有问题的部分)
然后,创建一个障碍物,其方向end
引用每行的所有前部分。将每行的所有第二部分相对于屏障定位,并添加一些margintart
,以在第一部分和第二部分之间创建空间。第二部分包含冒号和动态生成的内容
这里是最后一个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>
如中所述,正是制表符(\t
)导致布局出现问题。只需删除制表符并用几个空格替换即可。但是,由于我使用的字体不是等距的,冒号部分没有完全对齐
另一种解决方案是将每行一个文本视图分成两个文本视图。这也对我有利,因为现在,我可以以多种语言提供字符串文字(例如,公司
,位置
,等等),并将动态生成的内容(从数据绑定用户
)添加到整体显示的文本中。然后,在所有分隔之后,将文本视图放置在ConstraintLayout中,替换原始线性布局。现在,我们的结构如下(对于有问题的部分)
然后,创建一个障碍物,其方向end
引用每行的所有前部分。将每行的所有第二部分相对于屏障定位,并添加一些margintart
,以在第一部分和第二部分之间创建空间。第二部分包含冒号和动态生成的内容
这里是最后一个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>
为什么不将宽度设置为与父项匹配?@tyczj哪个宽度?线性布局?线性布局的子级。在我看来,图片和旁边文本的填充导致了这个问题。不要在textview中设置drawable,只需创建一个ImageView
,将文本设置为没有图像的文本并重新对齐即可everything@tyczj使用可绘制视图设置文本视图的宽度会使文本居中,但会使可绘制视图与左侧对齐。我想画(图标)和文本是有并排。但是,我将尝试您的建议,使用单独的ImageView
和TextView
创建行。虽然我更希望我能用一个TextView
@tyczj,但我已经试过了你的建议。这是我试过的