TextView gravity not working on API 18 or higher

点点圈 提交于 2019-12-10 23:37:59

问题


I have an TextView wich overlaps some other view. Everything works great, except the gravity on API equal or higher than 18.

Here is my TextView:

<TextView
    android:id="@+id/overlappingTextView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_alignBottom="@id/behindLinearLayout"
    android:layout_alignLeft="@id/behindLinearLayout"
    android:layout_alignRight="@id/behindLinearLayout"
    android:layout_alignTop="@id/behindLinearLayout"
    android:background="@drawable/overlapping_bg"
    android:gravity="center"
    android:text="Waiting for user confirmation..."
    android:textColor="@color/blue" />

The gravity works ok on 4.2.2 or lower but is not working on 4.3 and higher ... It works only partially, it is centered but only vertically.

LE:

There is no fix for this issue? Or does anyone know why this is happening?


回答1:


This appears to be a known issue in API 18+, and there's a workaround involving wrapping your view in a LinearLayout.

See the answer in this thread.

In your case, the fixed code should look something like:

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_alignBottom="@id/behindLinearLayout"
    android:layout_alignLeft="@id/behindLinearLayout"
    android:layout_alignRight="@id/behindLinearLayout"
    android:layout_alignTop="@id/behindLinearLayout"
    android:background="@drawable/overlapping_bg" >

    <TextView
        android:id="@+id/overlappingTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Waiting for user confirmation..."
        android:textColor="@color/blue" />

</LinearLayout>


来源:https://stackoverflow.com/questions/21658826/textview-gravity-not-working-on-api-18-or-higher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!