我在中间有一个TextView,左边有一个TextView。我希望左侧的TextView始终为WRAP_CONTENT
,并且不要与中间的TextView重叠。下面是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/Widget.AppCompat.Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/tv_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginEnd="4dp"
android:layout_marginStart="16dp"
android:layout_toStartOf="@id/tv_middle"
android:background="#55ff0000"
android:padding="6dp"
android:singleLine="true"
android:textColor="@color/menuRedColor"
android:textSize="18sp"
tools:text="Back" />
<TextView
android:id="@+id/tv_middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="20sp"
tools:text="This is Title" />
</RelativeLayout>
问题是左边的TextView总是占据左边的所有空间,而不是WRAP_CONTENT
。这是屏幕截图。
如果我删除左侧TextView中的layout_toStartOf
属性,WRAP_CONTENT
将工作得很好,但如果中间的TextViews内容变长,两个TextView将会重叠。
有人能帮上忙吗?非常感谢!
发布于 2018-05-11 17:47:50
tv_left
android:layout_alignParentLeft="true"
android:layout_toStartOf="@id/tv_middle"
因为2这个特征,所以一个长度长度从parent的左边到tv_middle
。您必须在此功能2中删除一个,因为textview
可能为wrap_content
。对不起,我的英语不好
发布于 2018-05-11 17:48:44
我在这里更新了你的代码检查,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/Widget.AppCompat.Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/tv_middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="20sp"
tools:text="This is Title" />
<TextView
android:id="@+id/tv_left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="4dp"
android:layout_marginStart="16dp"
android:layout_toStartOf="@id/tv_middle"
android:background="#55ff0000"
android:padding="6dp"
android:singleLine="true"
android:textColor="@color/menuRedColor"
android:textSize="18sp"
tools:text="Back" />
</RelativeLayout>
将文本视图设置为另一个文本视图的开始后,将文本视图的宽度设置为match_parent。它不会与中间的那个重叠。
发布于 2018-05-11 17:49:20
变化
android:layout_toStartOf="@id/tv_middle"
至
android:layout_alignLeft="@id/tv_middle"
https://stackoverflow.com/questions/50289303
复制相似问题