首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用最大宽度的wrap_content?

如何使用最大宽度的wrap_content?
EN

Stack Overflow用户
提问于 2015-08-16 06:29:46
回答 11查看 35.8K关注 0票数 32

我试图布局一个视图,应该包装它的内容,但它不应该超过100 it小于它的父宽度。我如何使用RelativeLayout或其他布局来做到这一点?我现在所拥有的,总是会使视图100 so比它的父视图少,这样就有了另一个视图的空间。

这张照片就是我所拥有的一个例子:

正如你所看到的,文本没有填满整个盒子,所以它可能更小。但是,它不应该比它的父消息少100 so,这样消息发送时就有空间了。

这是我的布局。

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipChildren="false"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    android:paddingRight="@dimen/horizontalMargin"
    android:paddingTop="10dp">


    <TextView
        android:id="@+id/message_holder"
        android:layout_toLeftOf="@id/blank"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/horizontalMargin"
        android:background="@drawable/message_corners"
        style="@style/white_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="alsdkjf; alsdkf" />

    <RelativeLayout
        android:id="@+id/blank"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_alignParentRight="true"
        android:minWidth="100dp">

    </RelativeLayout>

    <TextView
        android:minWidth="100dp"
        android:id="@+id/time"
        style="@style/gray_text"
        android:layout_toRightOf="@id/message_holder"
        android:paddingLeft="10dp"
        android:text="Yesterday,\n11:30 PM" />


    <RelativeLayout
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignBottom="@id/message_holder"
        android:layout_alignParentLeft="true"
        android:background="@drawable/triangle" />

</RelativeLayout>

我尝试在消息框右侧的空白视图上使用"minWidth“属性来提供间隔,但是它不会调整大小以变大(这会使消息框更小)。如果我没有空视图,只需将时间TextView放在消息框的右侧,那么当消息框展开时,TextView将不可见。

更新:

这是我的"message_corners.xml":

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">


    <solid
        android:color="@color/green" >
    </solid>


    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp"    >
    </padding>


    <corners
        android:radius="10dp">
    </corners>

</shape>

更新2:

这就是我在一个简短的文本布局中所要寻找的:

这就是我在一个长文本布局中所要寻找的:

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2015-08-16 06:51:16

给你,一个你想要的布局。

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipChildren="false"
    android:paddingBottom="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp">


    <RelativeLayout
        android:id="@+id/blank"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#aaaaaa">
        <LinearLayout
            android:id="@+id/message_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="100dp">
            <TextView
                android:id="@+id/message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello?"
                android:background="#00ff00" />
        </LinearLayout>

        <TextView
            android:id="@+id/time"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/message_container"
            android:layout_marginLeft="-100dp"
            android:text="12:30 PM"
            android:background="#ff0000" />
    </RelativeLayout>

</RelativeLayout>

短消息

长消息

票数 26
EN

Stack Overflow用户

发布于 2018-01-11 04:39:58

我知道这是一个很老的问题,但这是一个令人沮丧的问题,我已经多次遇到,现有的答案并不完全是我想要的。我和一些同事提出了以下建议:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#FFFFFF">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#888888"
        android:padding="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FF00"
            tools:text="Short message."/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="0"
            android:background="#CCCCCC"
            tools:text="Yesterday,\n11:30pm"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#888888"
        android:padding="10dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/message_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#00FF00"
            tools:text="Super ultra mega awesome long message which is going to help us take over the world."/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="0"
            android:background="#CCCCCC"
            tools:text="Yesterday,\n11:31pm"/>

    </LinearLayout>

</LinearLayout>

呈现时如下所示:

神奇之处似乎是右边文本框的权重为零(除了左边文本框的非零权重值外,其他一些答案已经有了)。

老实说,我无法确切地解释它的工作原理,但在寻找了这么长时间的解决方案之后,我并没有质疑它!)

顺便说一下,我喜欢这种方法,因为它不需要任何显式或最小宽度、任何中间包装视图,也不需要使用裁剪设置、边距、填充等来实现视图覆盖。

票数 11
EN

Stack Overflow用户

发布于 2015-08-16 06:43:24

这个问题的作者真正提出的问题是,如何让TextView扩展以适应其中的消息,而不溢出时间TextView,并且不留下空白。

由于您实际上不知道整个屏幕的宽度,所以您不能告诉您的TextView比它少100 it。您应该做的是将您的TextView包装在一个具有toLeftOf规则的容器中,而TextView只包装它的内容。这样,容器将向右展开(而不溢出时间TextView) ,但是TextView只会包装它的文本内容(因此,它不会扩展任何空格)

而不是

代码语言:javascript
运行
复制
<TextView
        android:id="@+id/message_holder"
        android:layout_toLeftOf="@id/blank"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/horizontalMargin"
        android:background="@drawable/message_corners"
        style="@style/white_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="alsdkjf; alsdkf" />

使用

代码语言:javascript
运行
复制
<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"       
 android:layout_toLeftOf="@id/blank"
 android:layout_alignParentLeft="true"
 android:layout_marginLeft="@dimen/horizontalMargin">

     <TextView
          android:id="@+id/message_holder"
          android:background="@drawable/message_corners"
          style="@style/white_text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="alsdkjf; alsdkf" />
</LinearLayout>

顺便说一下,你的布局不是很好。你应该优化它。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32032468

复制
相关文章

相似问题

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