gpt4 book ai didi

android - EditText 填充属性对 API 19 和 21 的作用不同

转载 作者:太空宇宙 更新时间:2023-11-03 12:51:59 25 4
gpt4 key购买 nike

我附上了运行 19 和 21 模拟器的屏幕截图: enter image description here

左边是 Android 19,右边是 21。如您所见,基于 API 19 的模拟器不遵守 padding 属性。同样的事情发生在 4.4.x 设备上,所以这不仅仅是一个模拟器问题。

这是我用于此 Activity 的 .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>

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

最佳答案

这似乎是 API 21 中的错误 - Issue 77982 .

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

更新:1) 实际上,21 工作正常。是不是19.

错误本身是——“填充在 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);

关于android - EditText 填充属性对 API 19 和 21 的作用不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28700938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com