2016-05-24 119 views
1

我想創建一個自定義列表項目佈局,但我遇到了問題。看下面的例子:Android佈局與wrap_content

有效運作

Effective functioning

的問題

The Issue

問題是,當綠色矩形填充所有的藍色框(寬度設置爲wrap_content),因爲綠色的廣場消失了。廣場必須是綠色矩形的右邊,但這個矩形的大小可以改變,我不能將綠色方塊固定在藍色框的右邊。

紅色的矩形是固定的,它們不是問題。

我使用一些LinearLayoutRelativeLayout來做到這一點,但它不工作,因爲我希望。 我也不能使用drawableRight屬性,因爲在不久的將來,會有兩個圖標(綠色方塊)而不是一個。

如果有人已經遇到這個問題或有辦法解決這個問題。

的通緝的結果是:

[ListIconView] [[一些文本這裏] [圖標] < .....空白.....> [X] [Y] [Z] ]

[[ListIconView] [[長文本來自.......] [圖標]] <。空格...> [x] [y] [z]]

[[ListIconView] [[來自舊書的非常長的文本....] [圖標]] [x] [y] [z ]]

x,y和z是圖標。

+1

請包含一些代碼。 –

+1

請添加相關的佈局文件。 –

+0

編輯初始文章 – Cylon

回答

0

假設紅色正方形和長方形都沒有問題(可能父佈局RelativeLayout),你有TextViewImageView綠色長方形,廣場綠地,使用

android:layout_weight="1" 

TextView(綠色矩形)

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Some very looooooooooooooooooooooooooooooooooooooooooooooooong text here" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/my_image" /> 

</LinearLayout> 
0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="100"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="20" 
     android:text="blablah"/> 

    <LinearLayout 
     android:layout_weight="60" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:weightSum="100"> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="80" 
      android:text="test string string string"/> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="20" 
      android:text="String test"/> 

    </LinearLayout> 

    <ImageButton 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="20"/> 

</LinearLayout> 

我上面做的是我基於百分比爲每個我的空間分配我希望能夠適應LinearLayout的根佈局,超過總重量100%。 ImageView將佔用20%的空間,接下來是佔用60%的另一個LinearLayout,最後是佔用20%的ImageButton。