对齐适配器行元素

时间:2013-01-11 15:34:12

标签: android android-layout

我正在尝试在列表视图中对齐行元素。 我有一个布局行xml文件,其中包含以下代码:

<?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:id="@+id/TV_list_from_db_p_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp" >

        <Button
            android:id="@+id/BTN_addOne_from_db"
            android:layout_width="33dp"
            android:layout_height="wrap_content"
            android:background="@drawable/button"
            android:onClick="addOneToQuantity"
            android:text="+" />

        <EditText
            android:id="@+id/TV_Quantity_from_db"
            android:layout_width="44dp"
            android:layout_height="32dp" >
        </EditText>

        <Button
            android:id="@+id/BTN_subOne_from_db"
            android:layout_width="33dp"
            android:layout_height="wrap_content"
            android:background="@drawable/button"
            android:onClick="subOneToQuantity"
            android:text="-" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/CHK__list_from_db"
        android:layout_width="30dp"
        android:layout_height="30dp"
  />

</LinearLayout>

This code gives me this:                 我想要的元素 要对齐,现在它与文本视图的长度对齐

像这样:
| TextView |数字选择器|复选框|
| TextView |数字选择器|复选框|
| TextView |数字选择器|复选框|

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

由于您使用的是LinearLayout,因此可以将内部LinearLayout和CheckBox都设为layout_width="wrap_content",并提供TextView layout_width="0dp"layout_weight="1"

答案 1 :(得分:0)

使用相对布局并按以下顺序排列:

Check box - align parent right - width="30dp"
Button1 - to left of check box - width="33dp"
Edit Text - to left of Button1 - width="44dp"
Button2 - to left of Edit Text - width="33dp"
TextView - align parent left and to left of Button2 - width wrap_content