0

I am attempting to vertically centre a TextView element within a listview row. The left hand side of the image shows the list view, in the first view the textview has the value "MSH"

As the image shows this is not quite in the centre, it is slightly too high.

The layout view for the textview row is shown below.

How do I get the textview centred?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:tools="http://schemas.android.com/tools"                                       android:orientation="horizontal"
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_marginRight="5dp"
xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView android:id="@+id/tvName" 
            android:gravity="center_vertical"
            android:layout_width="95dp" 
            android:layout_height="40dp" 
            android:layout_marginLeft="10dp" 
            android:text="XXX"
            android:textSize="30sp"  
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView android:id="@+id/tvLongName"
            android:layout_toRightOf="@+id/tvName"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="dfdfdsfdfdfdsfdsf" 
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView android:id="@+id/tvUsage" 
            android:layout_below="@+id/tvLongName"
            android:layout_toRightOf="@+id/tvName"
            android:layout_height="wrap_content" 
            android:layout_width="10dp"
            android:layout_marginRight="5dp"
            android:gravity="right"
            android:text="R" 
            android:textAppearance="?android:attr/textAppearanceMedium" />  

        <TextView android:id="@+id/tvCardinality" 
            android:layout_below="@+id/tvLongName"
            android:layout_toRightOf="@+id/tvUsage"
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true" 
            android:layout_marginRight="5dp"
            android:text="(0..1)" 
            android:textAppearance="?android:attr/textAppearanceMedium"  />

   </RelativeLayout>

Screenshot

4

1 回答 1

1

而不是使用android:gravity="center_vertical"

你应该使用android:layout_centerVertical="true"

那肯定会成功的。

于 2012-11-17T05:08:01.470 回答