相对布局和带有无法工作重力的文本视图

4
我有一个简单的列表项布局,用于显示图像和标题。
<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>

我的问题是文本重心应该是"center-vertical"。当我在开发者设置中显示视图边界时,可以看到TextView的大小大于文本本身,但重力仍然保持在顶部。这个问题出现在我的Android KitKat设备上,但在例如Android Froyo上却没有。


尝试将TextView的高度设置为与其父级匹配,并保留android:gravity="center_vertical" - Phantômaxx
4个回答

2
这是一个已经被修复的已知问题,它不会影响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>

0

android:gravity="center_vertical" 改为 android:layout_centerInParent="true"

只需使用此布局,它就可以正常工作。

<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" />

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@+id/image"
                android:layout_centerInParent="true"
                android:text="Title" />
</RelativeLayout>

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

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center_vertical">

   <ImageView
    android:id="@+id/image"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@drawable/ic_launcher" />

   <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Title" />

</LinearLayout>

谢谢您的帮助,但是我想用RelativeLayout来完成它,因为我已经将问题分解为最简单的形式,但是我的布局中有超过8个子视图。 - Happo

0

请检查此布局代码并

image

<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/ic_launcher" />

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_toRightOf="@+id/image"
                android:layout_centerInParent="true"
                android:text="@string/hello_world" />
            </RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接