2011-09-03 46 views
0

我有一個自定義列表佈局。列表中的內容應該是,Android中的佈局處理

 
+--------------+-------------+------------+ 
|an image icon | text label |a checkbox | 
+--------------+-------------+------------+ 
  • 圖像應始終左對齊
  • 複選框應始終右對齊。
  • 文本標籤應占用剩餘空間。

我的問題是,複選框沒有對齊到屏幕右側。它根據文本標籤的大小佔據該位置。 如果標籤太大,複選框從不顯示/一半顯示。

佈局文件示例如下

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TableLayout android:id="@+id/tableLayout1" 
    android:layout_width="match_parent" android:layout_height="wrap_content"> 
    <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <ImageView android:id="@+id/imageView1" android:src="@drawable/icon" 
      android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> 
     <TableLayout android:id="@+id/tableLayout2" 
      android:layout_width="match_parent" android:layout_height="wrap_content"> 
      <TableRow android:id="@+id/tableRow5" android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
       <TextView android:layout_width="wrap_content" 
        android:text="TextView" android:id="@+id/textView1" 
        android:layout_height="wrap_content"></TextView> 
      </TableRow> 
      <TableRow android:id="@+id/tableRow6" android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
       <TextView android:text="TextView" android:id="@+id/textView2" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
      </TableRow> 
      <TableRow android:id="@+id/tableRow7" android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
       <TextView android:text="TextView" android:id="@+id/textView3" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
      </TableRow> 
      <TableRow android:id="@+id/tableRow8" android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
       <TextView 
        android:text="x" 
        android:id="@+id/textView4" android:layout_width="wrap_content" 
        android:layout_height="wrap_content"></TextView> 
      </TableRow> 
     </TableLayout> 
     <CheckBox android:text="" android:id="@+id/checkBox1" 
      android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox> 
    </TableRow> 

</TableLayout> 

</LinearLayout> 

回答

2

使用本

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relativeLayout1"> 

     <ImageView android:id="@+id/imageView1" 
    android:layout_height="wrap_content" android:layout_width="wrap_content" 
    android:src="@drawable/icon" android:layout_alignParentTop="true"></ImageView> 
     <CheckBox android:id="@+id/checkBox1" 
    android:layout_alignParentTop="true" android:text="CheckBox" 
    android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true"></CheckBox> 
     <TextView android:text="@string/hello" android:id="@+id/TextView1" 
     android:layout_toRightOf="@id/imageView1" android:layout_toLeftOf="@id/checkBox1" 
    android:layout_height="wrap_content" android:layout_width="fill_parent"></TextView> 

    </RelativeLayout> 
+0

這解決了我的問題。謝謝。 – Tyrant

0

如果妳解決的TextView的寬度,則複選框會在正確的,或者如果u插入TextView的一些長文則僅烏爾複選框進來right.You給WRAP_CONTENT對於寬度多數民衆贊成Ÿ它不會來正確....

另一種選擇,就是你可以使用相對佈局,並相應設置組件....

可能我的解決方案有助於ü。

萬事如意

+0

感謝您的答覆。修正textview的寬度可能會導致UI失真,因爲android設備來自不同的分辨率。我會嘗試相對佈局。 – Tyrant

3

播放與layout_weight財產 並嘗試此示例:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <ImageView 
       android:layout_width="fill_parent" 
       android:src="@drawable/icon" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"> 
     </ImageView> 
     <LinearLayout 
       android:layout_width="fill_parent" 
       android:orientation="vertical" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"> 
      <TextView 
        android:layout_width="fill_parent" 
        android:text="some text 1" 
        android:layout_height="wrap_content"> 
      </TextView> 
      <TextView 
        android:layout_width="fill_parent" 
        android:text="some text 2" 
        android:layout_height="wrap_content"> 
      </TextView> 
     </LinearLayout> 
     <CheckBox 
       android:layout_width="fill_parent" 
       android:layout_weight="1" 
       android:layout_height="wrap_content"> 
     </CheckBox> 
    </LinearLayout> 
</LinearLayout>