0

這是目前我的列表視圖排佈置:的LinearLayout:如何正確包裝,沒有線較長的文本打破

<?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="match_parent" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:layout_marginLeft="5dp" 
     android:text="99.99.9999" 
     android:layout_weight="0.2"/> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_weight="0.6"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:text="This is a correct looking text" 
      android:id="@+id/txtComment"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.2"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:layout_marginTop="10dp" 
      android:textStyle="bold" 
      android:layout_marginRight="5dp" 
      android:text="-1234,56" /> 
    </LinearLayout> 
</LinearLayout> 

問題是與ID txtComment TextView的範圍內。對於包含常規換行符的短文本內容和文本內容,一切都很好看。

但是,只要沒有換行符的長文本,Android仍然會包裝文本,但也會改變周圍LinearLayout的權重,以便左右文本視圖被壓縮。

如何確保對於沒有換行符的長文本Android仍尊重元素的權重?

截圖顯示的問題:

Short text Longer text with line breaks Longer text without line breaks

回答

2

試試這個,

,因爲你已經設置寬度爲wrap_content的權重都不能正常工作。將其更改爲0dp

<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:layout_marginLeft="5dp" 
    android:text="99.99.9999" 
    android:layout_weight="0.2"/> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_weight="0.6"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:text="This is a correct looking text" 
     android:id="@+id/txtComment"/> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.2"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="right" 
     android:layout_marginTop="10dp" 
     android:textStyle="bold" 
     android:layout_marginRight="5dp" 
     android:text="-1234,56" /> 
</LinearLayout> 

+1

這個工作很完美,謝謝 – devnull69

1

你只需要設置你的android:layout_width="wrap_content"android:layout_width="0dip",你的權重。

<?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="match_parent" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="10dp" 
     android:layout_weight="0.2" 
     android:text="99.99.9999" /> 

    <LinearLayout 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.6" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/txtComment" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.2"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="10dp" 
      android:gravity="right" 
      android:text="-1234,56" 
      android:textStyle="bold" /> 
    </LinearLayout> 
</LinearLayout> 

enter image description here

+1

太好了,謝謝。我從來沒有聽說過有關此事的任何事有用的信息 – devnull69

+0

在這裏閱讀文檔:https://developer.android.com/guide/topics/ui/layout/linear.html –

0

的事情是,:-)你設定的權重爲20%,60%和20%,這是過去的情況下,究竟發生了什麼佈局工作好了。

另外,使用LineraLayout權重時,您應該設置android:layout_width="0dp"

0

在你的根元素:android:weightSum="1"

在你的子元素android:layout_width="0dp"

0

有更好的和更清潔的方式來實現這一目標:

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:padding="4dp" 
    app:alignmentMode="alignBounds" 
    app:columnCount="3" 
    app:rowOrderPreserved="false" 
    app:useDefaultMargins="true"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="10dp" 
     android:text="99.99.9999" 
     app:layout_gravity="fill_horizontal" /> 

    <TextView 
     android:id="@+id/txtComment" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text" 
     app:layout_columnWeight="3" 
     app:layout_gravity="fill_horizontal" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="10dp" 
     android:gravity="right" 
     android:text="-1234,56" 
     android:textStyle="bold" 
     app:layout_gravity="fill_horizontal" /> 
</android.support.v7.widget.GridLayout> 

也不要忘了在添加gradle文件:

compile 'com.android.support:gridlayout-v7:24.1.0'

enter image description here

相關問題