2015-08-16 35 views
12

我想布局应该包装其内容的视图,但它不应该比其父宽度少100多pd。我如何使用RelativeLayout或其他布局来做到这一点?我现在所拥有的东西总是会使视图100dp的数量少于它的父级,这样就有了另一个视图的空间。如何使用最大宽度的wrap_content?

这张照片是什么,我有一个例子:

enter image description here

正如你所看到的,文本不能填满整个盒子,所以它可能是较小的。但是,它不应该比其父母大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:

这是我在简短的文字布局寻找:

enter image description here

这就是我在寻找的布局长文本:

enter image description here

回答

10

在这里你去,那不正是你想要的布局。

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

长消息 Long message

0

据我了解你想布局或TextView的为100 DP比屏幕尺寸 您可以通过获取屏幕宽度在做像素少做本

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int width = size.x; 
int height = size.y; 

然后,你可以设置textbiew宽度是从希望的屏幕大小这个帮助 诗我想你可能要DP转换为像素100dp少,但我不知道

+0

你能解释一下吗?我正在设置消息框的最大宽度,但我不知道这个宽度是多少,因为它是为了填满整个屏幕。 –

+0

添加的最大高度为message_holder TextView的 –

+0

我觉得他不关心高度 – Bhargav

0

如果你想在右边和文本消息在其左侧时间的文字,你可以做这样的事情(在相对布局使用这个)你也可以使用maxWidth没有minWidth

<TextView 
     android:id="@+id/view_textView_timeText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true"/> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/view_textView_timeText" 
     android:maxWidth="100dp"/> 
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" /> 

使用

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

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

+0

嗯,这个答案的一半,但你会如何去立即显示在右边的时间? –

+0

在这种情况下,的LinearLayout更改为水平,加上时间的TextView到它,所以它出现在消息的TextView的右侧,添加的LinearLayout一个marginRight的100dp,留出空间,当它完全展开。 – Eran

0

你可以做的就是把2次之间的空视图,并保持它的宽度MATCH_PARENT和分配的TextView到leftof这个空视图和空视图向左日期视图。只要确保保持视图为空。

1

您可以尝试的观点如下安排,其宽度:

<?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:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="6dp" 
     > 
    <FrameLayout 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content"> 
     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@android:color/holo_blue_bright" 
       tools:text="Some long test is this which is support to wrap at the end of parent view" 
       /> 
    </FrameLayout> 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginStart="6dp" 
      tools:text="Yesterday,\n 11:30 PM" 
      /> 
</LinearLayout> 
+0

这是非常接近,除了当文本较短,那么时间停留在,而不是被以第一TextView的冲水的权利。 –

0

你可以这样做(不问题的直接答案):

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="bottom" 
    android:orientation="horizontal"> 


    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="end" 
      android:maxLines="1" 
      android:paddingLeft="45px" 
      android:text="asdfadsfsafasdfsakljkljkhjhkkhjkjhjkjhjkhjkhljkhlkhjlkjhljkhljkhlkjhljkhljkhlfasd" 
      android:textColor="#4a4a4a" 
      android:textSize="40px" /> 
    </LinearLayout> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:maxLines="1" 
     android:paddingLeft="45px" 
     android:paddingRight="48px" 
     android:text="2017.08.09 13:00" 
     android:textColor="#9b9b9b" 
     android:textSize="34px" /> 
</LinearLayout> 
0

我知道这是一个非常古老的问题,但它是我现在已经遇到好几次令人沮丧的问题和现有的答案是不完全是我一直在寻找。一些同事和我想出了以下内容:

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

渲染时,看起来像这样:

enter image description here

魔术似乎是文本框的重量零值正确的(除了左侧文本框的非零权值,其他一些答案已经有了)。

老实说,我无法解释到底为什么它的工作原理,但在已经找了一个解决方案来了这么久,我不怀疑它! :)

顺便说一句,我喜欢这种方法,因为它不需要任何明确的或最小的宽度,任何中间包装视图,或使用剪辑设置,边距,填充等来实现视图叠加。

相关问题