【发布时间】:2015-08-16 06:29:46
【问题描述】:
我正在尝试布局一个应该包装其内容的视图,但它不应比其父宽度小约 100dp。如何使用 RelativeLayout 或其他布局来做到这一点?我现在所拥有的总是会使视图比其父视图小 100dp,以便为另一个视图留出空间。
这张照片是我所拥有的一个例子:
如您所见,文本不会填满整个框,因此它可能会更小。但是,它不应比其父级小 100dp,以便为发送消息的时间留出空间。
这是我的布局。
<?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”:
<?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:
这就是我在短文本布局中寻找的内容:
这就是我在带有长文本的布局中寻找的内容:
【问题讨论】: