Das*_*hir 20 android android-layout
我正在尝试布局应该包装其内容的视图,但它不应该比其父宽度小~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>
Run Code Online (Sandbox Code Playgroud)
我已尝试在消息框右侧的空白视图上使用"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>
Run Code Online (Sandbox Code Playgroud)
更新2:
这是我在短文本布局中寻找的内容:
这就是我在长文本布局中寻找的东西:
Dav*_*idH 19
在这里,您可以使用您想要的布局.
<?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>
Run Code Online (Sandbox Code Playgroud)
小智 5
这个问题的作者真正要求的是,如何让TextView扩展以适应其中的消息而不会浪费时间TextView,并且不留空格.
由于您实际上并不知道整个屏幕的宽度,因此您无法告诉TextView比它少100dp.你应该做的是将TextView包装在一个容器中,该容器具有toLeftOf规则,TextView只包装它的内容.这样,容器将一直向上扩展(没有溢出时间TextView)但TextView只会包装它的文本内容(因此,它不会扩展任何空格)
码
代替
<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" />
Run Code Online (Sandbox Code Playgroud)
使用
<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>
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你的布局不是很好.你应该优化它.
小智 5
我知道这是一个确实很老的问题,但是这是我几次遇到的令人沮丧的问题,现有的答案并不是我一直在寻找的。我和一些同事提出了以下建议:
<?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>
Run Code Online (Sandbox Code Playgroud)
呈现时如下所示:
魔术似乎是右侧文本框的权重为零值(除了左侧文本框的非零权重值之外,其他一些答案已经具有此值)。
老实说,我无法确切解释它为什么起作用,但是在寻找解决方案很长时间之后,我没有对此提出质疑!:)
顺便说一句,我喜欢这种方法,因为它不需要任何显式或最小宽度,任何中间包装视图,也不需要使用裁剪设置,边距,填充等来实现视图覆盖。
| 归档时间: |
|
| 查看次数: |
23101 次 |
| 最近记录: |