对齐reyclerview元素android

时间:2016-03-30 14:10:39

标签: java android

我有一个带有TextView和EditText的recyclerview,它构建了左边是TextView的行,右边是EditText。 目前的观看情况如下:

Aaa:  value1
Abbsbc:  value2
Cskhkdhd: value3
HJhk:  value4

我希望这个观点看起来像这样:

            Aaa:  value1
     Abbsbc:  value2
 Cskhkdhd:  value3
         HJhk:  value4

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/brand_row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/brand_selector">

<View
    android:id="@+id/divider"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/divider"/>

<TextView
    android:id="@+id/object"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/edit_box_height"
    android:layout_below="@id/divider"
    android:gravity="center_vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="@color/noData"/>

<EditText
    android:id="@+id/value"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/edit_box_height"
    android:layout_below="@id/divider"
    android:layout_toRightOf="@id/object"
    android:background="@android:color/transparent"
    android:hint="Enter value"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="@android:color/black"
    android:textColorHint="@color/divider"/>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

在Textview中添加以下行:

android:textAlignment="viewEnd" 
android:gravity="center_vertical|end"

另外我认为如果textview文本的长度在行中变化,这仍然会导致对齐不均匀。因此,如果使用“线性布局”作为父布局,则会更好。我相应地重做了xml:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout
        android:id="@+id/brand_row"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/brand_selector">

        <TextView
            android:id="@+id/object"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="@dimen/edit_box_height"
            android:textAlignment="viewEnd"
            android:gravity="center_vertical|end"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            android:textColor="@color/noData"/>

        <EditText
            android:id="@+id/value"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="@dimen/edit_box_height"
            android:layout_below="@id/divider"
            android:layout_toRightOf="@id/object"
            android:background="@android:color/transparent"
            android:hint="Enter value"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            android:textColor="@android:color/black"
            android:textColorHint="@color/divider"/>
    </LinearLayout>

答案 1 :(得分:0)

尝试使用权重的LinearLayout:

<RelativeLayout
    android:id="@+id/brand_row"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/brand_selector">

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/divider"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/divider"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/object"
            android:layout_width="0dp"
            android:layout_height="@dimen/edit_box_height"
            android:layout_weight="0.5"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            android:textColor="@color/noData"
            android:gravity="end"/>

        <EditText
            android:id="@+id/value"
            android:layout_width="0dp"
            android:layout_height="@dimen/edit_box_height"
            android:layout_weight="0.5"
            android:background="@android:color/transparent"
            android:hint="Enter value"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            android:textColor="@android:color/black"
            android:textColorHint="@color/divider"
            android:paddingStart="10dp"/>
    </LinearLayout>


</RelativeLayout>

编辑:我在textView右侧添加了一个重力END,在EditText上添加了一个Startpadding,以便在标签和值之间留出空间