【发布时间】:2014-07-08 20:04:44
【问题描述】:
我有一个 ListActivity 和一个使用 list_item.xml 的自定义 ArrayAdapter。在这个 xml 文件中,我有一个 ImageView 和三个 TextViews。
ImageView 和前两个TextViews 靠左对齐,第三个TextView 靠右对齐。这一切都按预期工作。
现在我还想将TextViews 对齐在ListItem 的中心。
现在我使用RelativeLayout,因此我可以将第三个TextView 向右对齐,并且我在TextViews' layout_height 上使用match_parent,因为我在某处读过它需要对齐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>
结果如下:
我想要的是:
编辑 1:
在三个TextView中将android:gravity="center_vertical"改为android:gravity="center"后,Text只水平居中对齐,垂直居中不对齐:
【问题讨论】:
-
改变重力=中心
-
@Sania 如果我在 TextViews 中将
gravity="center_vertical"更改为"center",则文本仅水平对齐到中心,而不是垂直对齐.. :S(参见编辑 1) -
抱歉改成水平居中
-
@KevinCruijssen 为 TextView 尝试 android:lineSpacingExtra
-
@Sania
center和center_horizontal给出相同的结果(编辑 1 中的图片),而center_vertical由于某种原因不起作用..
标签: android android-layout textview android-relativelayout vertical-alignment