2017-07-06 206 views
0

我尝试做布局有两个TextView的和一个线性布局:如何修复LineLayout的最后一个元素,使其不超出屏幕边界?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_orange_dark" 
     android:text="testsdddddddddddddddddddddddddddasdsdfsdfsdf"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_red_dark" 
     android:maxLines="1" 
     android:text="NOT HIDE TEXT"/> 
</LinearLayout> 

但是,当第一个元素有许多字符第二个文本是hidding。 Not need 但我想这样做: I need 当第一个文本短(或第二元素可以全部不填aviliable区): Normal mode 我可怎么办呢?可能是我需要使用RelativeLayout? 非常感谢您的回答)

回答

3

下面是怎么样的?你只需要添加另一个视图来填充“NOT HIDE TEXT”之后的空白区域,并且看起来完全一样。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="40dp"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/holo_orange_dark" 
      android:ellipsize="end" 
      android:lines="1" 
      android:text="testsdddddddddddddddddddddddddddasdsdfsdfsdf" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:background="@android:color/holo_red_dark" 
      android:text="NOT HIDE TEXT" /> 

    </LinearLayout> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@android:color/holo_red_dark" /> 

</LinearLayout> 
0
 To have 10dp of space at left between widgets use 
    android:marginLeft="10dp" 
    To have 10dp of space at Right between widgets use 
    android:marginRight="10dp" 

    or you can use android:margin for having 10dp space on all four co-ordinate of the widget. 

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_red_dark" 
     android:maxLines="1" 
     android:text="NOT HIDE TEXT"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_orange_dark" 
     android:text="testsdddddddddddddddddddddddddddasdsdfsdfsdf"/> 
</FrameLayout> 

which ever Widget will be last in hierarchy will be visible its like stack of widgets. 
+0

为了什么?我希望第二个元素不要缩小并且在屏幕后面。 –

+0

如果你想要第二个布局使用而不是Relative和LinearLayout,它们将不起作用。 –

+0

你能写例子吗? –

0

试试这个:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:layout_weight="1" 
     android:background="@android:color/holo_orange_dark" 
     android:text="testsdddddddddddddddddddddddddddasdsdfsdfsdf"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_red_dark" 
     android:maxLines="1" 
     android:ellipsize="end" 
     android:layout_weight="1" 
     android:text="NOT HIDE TEXT"/> 
</LinearLayout> 

现在无论textViews将占据屏幕宽度的一半。调整重量以增加宽度。

希望它有帮助!

+0

不,这个比例不起作用。我需要第二个视图占据所有剩余的可用空间。第一个视图的宽度不是恒定的 –

+0

然后使第一个视图包装内容,并删除重量和保持重量1为第二个viee – sumit

0

使用一个tableLayout并在该tableRow内部。将一个视图对齐父视图和其他视图以layout_alignParentRight。如果第一个视图的值为空,则第二个视图将对齐到左侧。如果不是,它将与第一视图的权利对齐。

<TableLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:shrinkColumns="0"> 

       <TableRow 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center_vertical"> 

        <TextView 
         android:id="@+id/tv_description" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_marginLeft="15dp" 
         android:layout_marginTop="1dp" 
         android:layout_toLeftOf="@+id/tv_read" 
         android:ellipsize="end" 
         android:singleLine="true" 
         android:textColor="@android:color/white" 
         android:textSize="15dp" 
         android:visibility="visible" /> 

        <TextView 
         android:id="@+id/tv_read" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignParentRight="true" 
         android:layout_alignTop="@+id/tv_description" 
         android:layout_marginLeft="15dp" 
         android:layout_marginRight="20dp" 
         android:paddingRight="5dp" 
         android:textColor="@android:color/white" 
         android:textSize="15dp" 
         android:visibility="visible" /> 
       </TableRow> 

      </TableLayout> 
0

@Shahin Mursalov是正确的,但没有必要采取两项LinearLayout只是一个就可以了。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="hrizontal"> 

    <TextView 
     android:id="@+id/kek1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="90dp" 
     android:background="@android:color/holo_orange_dark" 
     android:ellipsize="end" 
     android:maxLines="1" 
     android:text="tegetghrhrebbbbbbbbbbbbgetgetge" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_marginLeft="-90dp" 
     android:layout_toRightOf="@id/kek1" 
     android:background="@android:color/holo_red_dark" 
     android:gravity="center" 
     android:maxLines="1" 
     android:minWidth="120dp" 
     android:text="NOT HIDE TEXT" /> 
</RelativeLayout> 

输出

长文本

enter image description here

与短文本

enter image description here

+0

他需要红色填充空的空间,而不是橙色的。 –

+0

仍然没有必要采取额外的布局.. @НикитаКуликов看看我更新的答案 –

+0

更新一个也不是一个解决方案。你不能假设红色框内的文本总是会少于90dp的空间。 –

相关问题