约束布局-Android-避免重叠
我有一个约束布局重叠的问题。基本上,我在左边有一个图像,在右边有一个文本视图。我试图让文本视图中的文本显示在屏幕的中心,并且它的显示与预期的一样。但是当文本大小过大时,文本视图与图像视图重叠 如果将TextView left约束设置为ImageView的右侧,则不会发生重叠。但当文本较小时,文本不会出现在屏幕的中心 我试图从中实现解决方案。但是,这对我不起作用。请帮我解决这个问题约束布局-Android-避免重叠,android,android-xml,android-constraintlayout,Android,Android Xml,Android Constraintlayout,我有一个约束布局重叠的问题。基本上,我在左边有一个图像,在右边有一个文本视图。我试图让文本视图中的文本显示在屏幕的中心,并且它的显示与预期的一样。但是当文本大小过大时,文本视图与图像视图重叠 如果将TextView left约束设置为ImageView的右侧,则不会发生重叠。但当文本较小时,文本不会出现在屏幕的中心 我试图从中实现解决方案。但是,这对我不起作用。请帮我解决这个问题 <androidx.constraintlayout.widget.ConstraintLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imgBack"
android:layoutWidth="wrap_content"
android:layoutHeight="match_parent"
android:contentDescription="@null"
android:src="@drawable/image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvUserName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:ellipsize="end"
android:text="I have a problem when the text is so big. It's overlapping"
android:fontFamily="@font/proxima_nova_semibold"
android:gravity="center"
android:lines="1"
android:textColor="@color/white"
android:textSize="@dimen/header_sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
请尝试此解决方案
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="350dp"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imgBack"
android:layout_width="0dp"
android:layout_height="match_parent"
android:contentDescription="@null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/tvUserName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@tools:sample/backgrounds/scenic" />
<TextView
android:id="@+id/tvUserName"
android:layout_width="200dp"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:lines="1"
android:text="I have a problem when the text is so big. It's overlapping"
android:textColor="@color/white"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
它不再重叠:
有Type。< /P>我设置APP:LayOutJuxTimeStasytoStaseOf=“Prime:TeNVIEW”的原因是,当文本很小时,我希望文本出现在屏幕中间。问题是textview与imageview重叠,而不是相反。当您将文本设置在中间,且文本较大时,图像将没有空间。如果要同时显示图像视图和文本视图以及中间的文本,则必须设置文本视图的固定宽度。如果你同意的话,我会更新答案。如果文本很小,它是否会出现在屏幕中央(而不是文本视图)?我认为较小的文本不会出现在屏幕中央。它会向右倾斜。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imgBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:src="@mipmap/ic_launcher"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="I have a problem when the text is so big. It's overlapping"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imgBack"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
android:layoutWidth="wrap_content"
android:layoutHeight="match_parent"