Android没有在设备/模拟器上垂直居中TextView的文本(但在Android Studio的设计器视图中很好)(Android not centering TextView's te

系统教程 行业动态 更新时间:2024-06-14 16:59:17
Android没有在设备/模拟器上垂直居中TextView的文本(但在Android Studio的设计器视图中很好)(Android not centering TextView's text vertically on device/emulator (but fine though in Android Studio's designer view))

我有这个设置:

<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:id="@+id/postHeader"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CP" android:id="@+id/initialsView" android:layout_alignTop="@+id/avatarView" android:layout_alignLeft="@+id/avatarView" android:layout_alignBottom="@+id/avatarView" android:layout_alignRight="@+id/avatarView" android:background="@drawable/avatar_background" android:textColor="@color/white" android:gravity="center" android:textStyle="bold" android:textSize="16sp" /> <com.makeramen.roundedimageview.RoundedImageView app:riv_corner_radius="20dp" android:layout_width="40dp" android:layout_height="40dp" android:id="@+id/avatarView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="14dp" android:layout_marginLeft="20dp" android:layout_marginBottom="6dp" app:riv_border_color="@color/lightGray" app:riv_border_width="0.2dp" /> ... />

TextView正确对齐图像视图。 Android Studio中的一切都很好:

但是,当我在实际设备(Android 4.4.4)或模拟器(Android 6.0)上运行时,我得到文本(屏幕截图中的“CP”)一直到顶部,就像我没有设置垂直引力。 我已经尝试将textAlignment ,width和height设置为match_parent , layout_centerInParent为both ,gravity设置为center_vertical explicity, center_vertical为false,但无效。 它仍然没有垂直居中(虽然水平很好)。

我究竟做错了什么? 为什么它在Android Studio上正确显示?

I have this setup:

<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:id="@+id/postHeader"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CP" android:id="@+id/initialsView" android:layout_alignTop="@+id/avatarView" android:layout_alignLeft="@+id/avatarView" android:layout_alignBottom="@+id/avatarView" android:layout_alignRight="@+id/avatarView" android:background="@drawable/avatar_background" android:textColor="@color/white" android:gravity="center" android:textStyle="bold" android:textSize="16sp" /> <com.makeramen.roundedimageview.RoundedImageView app:riv_corner_radius="20dp" android:layout_width="40dp" android:layout_height="40dp" android:id="@+id/avatarView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="14dp" android:layout_marginLeft="20dp" android:layout_marginBottom="6dp" app:riv_border_color="@color/lightGray" app:riv_border_width="0.2dp" /> ... />

The TextView aligns correctly to the image view. Everything is fine in Android Studio:

However, when I run on actual device (Android 4.4.4) or emulator (Android 6.0) I get the text ("CP" in the screenshot) stuck all the way to the top, just like I haven't set a vertical gravity. I've tried setting textAlignment, width and height to match_parent, layout_centerInParent to both, gravity to center_vertical explicity, includeFontPadding to false, but no avail. It still doesn't center vertically (horizontal is fine though).

What am I doing wrong? Why is it displayed correctly on Android Studio anyway?

最满意答案

好吧,经过一番调查后我发现了问题。 似乎它已被破坏: https : //code.google.com/p/android/issues/detail?id = 63673

在链接上实现解决方案解决了我的问题:

private static final int AT_MOST_UNSPECIFIED = MeasureSpec.makeMeasureSpec((1<<30)-1, MeasureSpec.AT_MOST); @Override protected void onMeasure(int widthSpec, int heightSpec) { // RelativeLayout has bugs when measured in UNSPECIFIED height mode that were supposedly // fixed in API 18. However the 'fix' does breaks gravity == center_vertical in TextView. // https://code.google.com/p/android/issues/detail?id=63673 if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED) heightSpec = AT_MOST_UNSPECIFIED; super.onMeasure(widthSpec, heightSpec); }

Okay I've figured out the problem after a bit investigation. Seems that it has been broken: https://code.google.com/p/android/issues/detail?id=63673

Implementing the solution at the link solved my problem:

private static final int AT_MOST_UNSPECIFIED = MeasureSpec.makeMeasureSpec((1<<30)-1, MeasureSpec.AT_MOST); @Override protected void onMeasure(int widthSpec, int heightSpec) { // RelativeLayout has bugs when measured in UNSPECIFIED height mode that were supposedly // fixed in API 18. However the 'fix' does breaks gravity == center_vertical in TextView. // https://code.google.com/p/android/issues/detail?id=63673 if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED) heightSpec = AT_MOST_UNSPECIFIED; super.onMeasure(widthSpec, heightSpec); }

更多推荐

本文发布于:2023-04-16 14:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/8978759eec870696bbf90c0ce3690907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:很好   但在   视图   器上   文本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!