首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓: layout_toStartOf不支持WRAP_CONTENT

安卓: layout_toStartOf不支持WRAP_CONTENT
EN

Stack Overflow用户
提问于 2018-05-11 17:33:35
回答 4查看 743关注 0票数 0

我在中间有一个TextView,左边有一个TextView。我希望左侧的TextView始终为WRAP_CONTENT,并且不要与中间的TextView重叠。下面是我的代码:

代码语言:javascript
复制
<?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将会重叠。

有人能帮上忙吗?非常感谢!

EN

回答 4

Stack Overflow用户

发布于 2018-05-11 17:47:50

tv_left

代码语言:javascript
复制
android:layout_alignParentLeft="true"
android:layout_toStartOf="@id/tv_middle"

因为2这个特征,所以一个长度长度从parent的左边到tv_middle。您必须在此功能2中删除一个,因为textview可能为wrap_content。对不起,我的英语不好

票数 0
EN

Stack Overflow用户

发布于 2018-05-11 17:48:44

我在这里更新了你的代码检查,

代码语言:javascript
复制
<?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。它不会与中间的那个重叠。

票数 0
EN

Stack Overflow用户

发布于 2018-05-11 17:49:20

变化

代码语言:javascript
复制
android:layout_toStartOf="@id/tv_middle"

代码语言:javascript
复制
android:layout_alignLeft="@id/tv_middle"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50289303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档