Doesn't work EditText padding in the API 21

烂漫一生 提交于 2019-12-03 05:56:41

This is android bug. In future it will be fix.

Bug report on google code.

Solved it by creating a custom edittext with padding and use it in xml.

public class MyEditTextView extends EditText{

public MyEditTextView(Context context) {
    super(context);
    init();
}

public MyEditTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MyEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init(){
    int paddingLeftRight = (int) getResources().getDimension(R.dimen.edittext_padding);
    int topPadding = this.getPaddingTop();
    int bottomPadding = this.getPaddingBottom();
    setPadding(paddingLeftRight, topPadding, paddingLeftRight, bottomPadding);
}

}

Just replace android:paddingLeft with android:paddingStart, as should be anyway and fixes this bug on those devices.

Try to give padding programmatically by using method setLayoutParams() it will work i am hopeful Ref: Example

Not really the best way to work around the problem, but it works for me: you can set android:layout_height attribute to some fixed value and set android:gravity to center or center_vertical

<EditText
        ...
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        .../>

Solved it in styles, using android standart edittext:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:editTextStyle">@android:style/Widget.EditText</item>
</style>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!