修复EditText在部分努比亚手机文字不居中问题

1.遇到一个很奇怪的兼容问题,EditText中输入的文字在部分努比亚手机上无法居中,其他手机无此问题。布局文件如下:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:hint="Hello World!"
        android:inputType="text"
        android:maxLines="1" />

</LinearLayout>

2.实际显示效果:


1182579-6225c94d5c46cdef.png
努比亚z18mini.png

1182579-0e491941800935d4.png
小米5.png

3.解决方法:
在代码中设置文字对齐方式(在布局文件中设置textAlignment是无效的):

   EditText editText = findViewById(R.id.textview);
   editText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

4.修改后效果:


1182579-95ccf9907053864f.png
努比亚z18mini修改后.png

猜你喜欢

转载自blog.csdn.net/weixin_34406796/article/details/87309078