gpt4 book ai didi

android - 重力无效的 RelativeLayout 和 TextView

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:23 24 4
gpt4 key购买 nike

我有一个简单的列表项布局来显示图像和标题。

<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"。当我在开发人员设置中显示 View 边界时,我可以看到 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>

关于android - 重力无效的 RelativeLayout 和 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24161033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com