我有这个设置:
<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); }更多推荐
发布评论