2011-05-30 57 views
0

我對listitem有以下linearlayout。Android LinearLayout包裝內容的問題

<ImageView android:id="@+id/icon" 
        android:src="@android:drawable/ic_input_add" 
        android:layout_height="wrap_content" 
        android:layout_width="22px" 
        android:layout_marginTop="4px" 
        android:layout_marginRight="4px" 
        android:layout_marginLeft="1px" /> 

    <LinearLayout android:id="@+id/linearLayout1" 
         android:orientation="vertical" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content"> 

     <TextView android:layout_width="wrap_content" 
         android:text="LongerTextView" 
         android:id="@+id/textView1" 
         android:layout_height="wrap_content"/> 

     <TextView android:layout_width="wrap_content" 
         android:text="TextView" 
         android:id="@+id/textView2" 
         android:layout_height="wrap_content"/> 

    </LinearLayout> 

    <LinearLayout android:id="@+id/linearLayout2" 
         android:layout_height="match_parent" 
         android:layout_width="fill_parent" 
         android:gravity="right"> 

     <ImageView android:layout_marginTop="4px" 
         android:src="@android:drawable/ic_input_add"  
         android:layout_marginLeft="1px" 
         android:layout_height="wrap_content" 
         android:layout_marginRight="1px" 
         android:id="@+id/icon2" 
         android:layout_width="22px"/> 
    </LinearLayout> 

該佈局應該顯示左側和一直在右側的圖像,兩個文本項之間。這工作正常,只要文字不會比屏幕寬。只要文字越長,我的右側圖標就會被推離屏幕。

該文本應該換行到一個新行,並且左右圖標都應該始終顯示。

我該如何做到這一點?

回答

2

嘗試使用Relative Layouts

裏面一個相對佈局,你可以指定一個圖像(ID:imgLeft爲例)與layout_alignParentLeft =「真」,將留在左邊

然後另一個圖像(imgRight)與layout_alignParentRight = 「真」,將留在右側

和然後將含有文本與線性佈局: layout_toLeftOf = 「@ ID/imgRight」 和layout_toRightOf = 「imgLeft」

0

IIRC(未經測試,但我可以在大約8小時內完成)可能會丟失第三個LinearLayout(包裝第二個ImageView的那個)。然後將包裝TextView的LinearLayout的android:gravity設置爲居中。

1

我已根據您的要求重新設計了您的xml。使用下面的代碼。

<?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="match_parent" 
    android:background="#ffffff"> 
    <ImageView 
     android:id="@+id/icon" 
     android:src="@android:drawable/ic_input_add" 
     android:layout_height="wrap_content" 
     android:layout_width="22px" 
     android:layout_marginTop="4px" 
     android:layout_marginRight="4px" 
     android:layout_marginLeft="1px"></ImageView> 
     <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_height="match_parent" 
     android:layout_width="wrap_content" 
     android:gravity="right" 
     android:layout_alignParentRight="true"> 
     <ImageView 
      android:layout_marginTop="4px" 
      android:src="@android:drawable/ic_input_add" 
      android:layout_marginLeft="1px" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="1px" 
      android:id="@+id/icon2" 
      android:layout_width="wrap_content"></ImageView> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:orientation="vertical" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_toRightOf="@+id/icon" 
     android:layout_toLeftOf="@+id/linearLayout2"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:text="LongerTextView" 
      android:id="@+id/textView1" 
      android:layout_height="wrap_content"></TextView> 
     <TextView 
      android:layout_width="wrap_content" 
      android:text="TextView" 
      android:id="@+id/textView2" 
      android:layout_height="wrap_content"></TextView> 
    </LinearLayout> 


</RelativeLayout> 

如果我的回覆對你有幫助,請不要忘記投票。

謝謝 Deepak