2015-06-06 40 views
0

難以獲得簡單的RelativeLayout。我建立一個排它應該是這樣組成的ListView:RelativeLayout&一個垂直分隔符在右邊對齊

+-----------------------------+--------------+ 
    | Line 1      | Small button | 
    | Line 2, more text   |    | 
    | Line 3, even more text  |    | 

這是我第一次嘗試 - 不幸的是,垂直分隔不出來。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:descendantFocusability="blocksDescendants"> 

    <TextView 
     android:id="@+id/lineOne" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:textColor="@color/wallet_holo_blue_light" 
     android:textSize="18dp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/lineTwo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@id/lineOne" 
     android:textSize="16dp" /> 

    <TextView 
     android:id="@+id/lineThree" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@id/lineTwo" 
     android:textSize="14dp" /> 

    <ImageButton 
     android:id="@+id/myButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="@android:color/transparent" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:onClick="doSomething" 
     android:src="@drawable/arrow_down" /> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="fill_parent" 
     android:layout_toLeftOf="@id/myButton" 
     android:background="?android:attr/listDivider" /> 

</RelativeLayout> 

編輯:我幾乎解決我自己的問題,下面的代碼。

現在我的佈局看起來不錯...

+-----------------------------+--------------+ 
| Line 1      | Small button | 
| Line 2, more text   |    | 
| Line 3, even more text  |    | 

...除非例如第一線過長 - 在這種情況下,分配器和按鈕不會顯示出來:

+-----------------------------+--------------+ 
| Line 1, way way way way too long which  | 
| wraps          | 
| Line 2, more text       | 
| Line 3, even more text      | 

您能否幫我做最後的調整?

非常感謝!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:descendantFocusability="blocksDescendants" 
    android:orientation="horizontal"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/lineOne" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@color/wallet_holo_blue_light" 
      android:textSize="18dp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/lineTwo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="16dp" /> 

     <TextView 
      android:id="@+id/lineThree" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="14dp" /> 

    </LinearLayout> 

    <!-- As per http://stackoverflow.com/questions/6992804/how-to-right-align-widget-in-horizontal-linear-layout-android --> 
    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal"> 

     <View 
      android:layout_width="1dp" 
      android:layout_height="fill_parent" 
      android:background="?android:attr/listDivider" /> 

     <ImageButton 
      android:id="@+id/myButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@android:color/transparent" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:src="@drawable/arrow_down" /> 

    </LinearLayout> 

</LinearLayout> 
+0

我查看了你的文件,它應該沒問題。我只看了預覽,但在我的情況下,垂直分頻器顯示。預覽中沒有看到任何分隔線?或者它也只在預覽中? – mrtn

+0

您還應該考慮將'layout_height'設置爲'wrap_content',因爲它們是listview項目。 – mrtn

+0

感謝您的關注!我在預覽中看到分頻器,但看不到我的手機。 – user3321335

回答

1
+-----------------------------+--------------+ 
| Line 1      | Small button | 
| Line 2, more text   |    | 
| Line 3, even more text  |    | 

如果你想達到這樣的佈局則首先想到這是固定長度,並且是可變的。我假設分隔線和小按鈕將始終有小寬度,只有線寬會改變。因此,在這種情況下,您可以設置按鈕和分隔線寬度,並將其餘寬度設置爲線寬。你可以使用layout_weight="1"來做到這一點,你的佈局將留下空間。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:descendantFocusability="blocksDescendants" 
    android:orientation="horizontal"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     //your lines here 
    </LinearLayour> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="fill_parent" 
     android:background="?android:attr/listDivider" /> 
    <ImageButton 
     android:id="@+id/myButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/transparent" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:src="@drawable/arrow_down" /> 
</LinearLayour>