我有一个简单的列表项布局来显示图像和标题.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/image" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/image"
android:layout_alignBottom="@+id/image"
android:layout_toRightOf="@+id/image"
android:gravity="center_vertical"
android:text="Title" />
</RelativeLayout>
我的问题是文本引力应该是“中心垂直”.当我在开发人员设置中显示视图边界时,我可以看到TextView大小比文本本身大,但重力保持在顶部.问题出现在我的Android KitKat设备上,但不会出现在Android Froyo上.
最佳答案 这是一个已经修复的
known issue,它不会影响Marshmallow(虽然我没有尝试过Lollipop). (不那么优雅)解决方案是将TextView包装在FrameLayout中:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/image" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/image"
android:layout_alignBottom="@id/image"
android:layout_toRightOf="@id/image"
android:layout_centerInParent="true">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Title" />
</FrameLayout>
</RelativeLayout>