2017-07-06 209 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的空間。 –

相關問題