对于API 19和21,EditText填充属性不起作用

Nac*_*tus 3 android android-layout

我正在附加一个运行的19和21个模拟器的屏幕截图: 在此输入图像描述

左边的那个是Android 19,右边的那个是21.你可以看到,基于API 19的模拟器不尊重padding属性.同样的事情发生在4.4.x设备上,因此它不仅仅是一个模拟器问题.

这是我用于此活动的.xml代码:

<RelativeLayout 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"
tools:context="com.birsan.cardbox.FolderScreen">

<ImageView
    android:id="@+id/magnifying_glass_icon"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignBottom="@+id/search_button"
    android:layout_alignLeft="@+id/search_folder"
    android:layout_alignStart="@+id/search_folder"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="9dp"
    android:layout_marginStart="9dp"
    android:contentDescription="magnifying glass"
    android:src="@drawable/ic_menu_search" />

<include
    android:id="@+id/toolbar"
    layout="@layout/app_bar" />


<EditText
    android:id="@+id/search_folder"
    android:layout_width="250dp"
    android:layout_height="30dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="10dp"
    android:background="@drawable/search_custom_widget"
    android:focusable="true"
    android:hint="Search in folders"
    android:iconifiedByDefault="false"
    android:paddingLeft="40dp"
    android:paddingRight="55dp"
    android:singleLine="true"
    android:textSize="14sp"

    android:textStyle="bold" />

<Button
    android:id="@+id/search_button"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="26dp"
    android:layout_alignBottom="@+id/search_folder"
    android:layout_alignEnd="@+id/search_folder"
    android:layout_alignRight="@+id/search_folder"
    android:layout_marginBottom="2dp"
    android:layout_marginRight="2dp"
    android:background="@color/my_blue"

    android:text="Go"
    android:textColor="#fff"
    android:textSize="10sp" />


<com.name.cardbox.SlidingTabLayout
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/app_bar"
    android:layout_marginTop="40dp"
    android:background="@color/my_blue" />


<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/sliding_tabs"
    android:background="#fff">


</android.support.v4.view.ViewPager>
Run Code Online (Sandbox Code Playgroud)

为什么会出现差异?我错过了什么,我该如何解决?谢谢.

Abh*_*k V 5

这似乎是API 21中的一个错误 - 问题77982.

应该在将来的版本中修复.对于临时修复,请尝试动态设置填充.

更新:1)Actually, the 21 works fine. Is the 19 that doesn't.

错误本身是 - "填充在api 21中工作正常,并且在较低的Android设备中不起作用".这个错误出现在sdk 21中,因为你用sdk 21编译你的应用程序,填充不能在较低的api中工作.

2) searchView.setPadding(40, 0, 55, 0);

这将以像素为单位设置填充,如果在高密度设备中运行,则不会注意到差异.尝试在dp中设置填充

int paddingLeft = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
        int paddingRight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 55, getResources().getDisplayMetrics());
        int paddingTop = editText.getPaddingTop();
        int paddingBottom = editText.getPaddingBottom();
        editText.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
Run Code Online (Sandbox Code Playgroud)