Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

android - How to use wrap_content with a maximum width?

I am trying to layout a view that should wrap its content, but it shouldn't be more than ~100dp less than its parent width. How can I do that using a RelativeLayout or some other layout? What I have right now will always make the view 100dp less than its parent so that there is space for another view.

This picture is an example of what I have:

enter image description here

As you can see, the text doesn't fill the whole box, so it could be smaller. But, it should never be larger than 100dp less than its parent, so that there is room for the time the message was sent.

This is my layout.

<?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,
11: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>

I have tried using the "minWidth" property on a blank view to the right of the message box to provide spacing, but it doesn't resize to be larger (which would make the message box smaller). When I don't have the blank view, and simply place the time TextView to the right of the message box, then that TextView isn't visible when the message box expands.

Update:

This is my "message_corners.xml":

<?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>

Update 2:

This is what I am looking for in a layout with short text:

enter image description here

And this is what I am looking for in a layout with long text:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here you go, a layout that does exactly what you want.

<?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>

Short message Short message

Long message Long message


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...