Android - ListItem RelativeLayout文本在TextViews的中心,高度为wrap_content

时间:2014-05-20 13:56:54

标签: android android-layout textview relativelayout vertical-alignment

我有一个ListActivity,其自定义ArrayAdapter使用list_item.xml。在这个xml文件中,我有一个ImageView和三个TextViews

ImageView和前两个TextViews彼此相邻排列,第三个TextView在右侧排列。这一切都是有意的。

现在我也希望将TextViewsListItem的中心对齐。

现在我使用了RelativeLayout,因此我可以将第三个TextView对齐到右边,我在match_parent使用TextViews' layout_height,因为我已经阅读了将TextView中的文字与RelativeLayouts中心对齐所需的地方。

这是我当前的list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- The View for a single CheckListItem -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:contentDescription="@string/checkbox_content_description"
        android:src="@drawable/checkbox_unchecked"
        android:background="@layout/transparent_background"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false" />

    <TextView
        android:id="@+id/tv_amount"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/image"
        android:layout_alignBottom="@id/image"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false" />

    <!-- NOTE: layout_toLeftOf has @+id/ instead of @id to create the R.id here -->
    <TextView 
        android:id="@+id/tv_product_name"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/tv_amount"
        android:layout_toLeftOf="@+id/tv_price"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_alignBottom="@id/image"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false"

        android:background="@layout/border" />
        <!-- TODO: Remove test border -->

    <!-- NOTE: id has @id instead of @+id, because we created the ID in the tv_product_name in the layout_toLeftOf -->
    <TextView
        android:id="@id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@id/image"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false" />

</RelativeLayout>

具有以下结果:

enter image description here

我想要的是:

enter image description here


编辑1:

在三个TextView中将android:gravity="center_vertical"更改为android:gravity="center"后,文本仅在水平居中对齐,但不是垂直对齐:

enter image description here

2 个答案:

答案 0 :(得分:1)

//Try this way,i hope this will help you to solve your problem...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:contentDescription="@string/checkbox_content_description"
        android:src="@drawable/checkbox_unchecked"
        android:background="@layout/transparent_background"
        android:focusableInTouchMode="false"
        android:adjustViewBounds="true"
        android:clickable="false"
        android:focusable="false" />
    <TextView
        android:id="@+id/tv_amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false" />


    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical">
    <TextView
        android:id="@+id/tv_product_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false"
        android:background="@layout/border" />
    </LinearLayout>

    <TextView
        android:id="@id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false" />

</LinearLayout>

答案 1 :(得分:0)

我刚用这个

编辑了你的一个xml.try
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
   android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
  <TextView  android:id="@+id/floraCompStat"
             android:layout_width="wrap_content"
             android:layout_height="fill_parent"
             android:layout_alignBottom="@+id/image"
             android:background="@drawable/bg"
             android:gravity="center_vertical"
             android:text="TextView" />

bg.xml

 <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        >
        <solid android:color="#FFFFFF"/>
        <stroke android:width="1sp" android:color="#FF0000" />
        <padding android:left="15dp"/>
    </shape>