我想要实现这种风格
如您所见,我的背景应用程序是纹理,因此我需要透明背景和 View 底部的几个渐变颜色像素。
我简单地用这个来描述我的EditText背景
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">>
<item
android:gravity="bottom"
android:height="2dp"
android:drawable="@drawable/my_gradient">
</item>
</layer-list>
它在 Android 6.0 和 Android M Preview 上运行良好,但在 Android 5.1.1 等“旧版本”上运行不佳
API 23 和 22(M) 与 API 22 及更早版本的渲染
我不明白为什么我的重力属性没有应用
最佳答案
LayerDrawable
子项的重力(和大小)属性似乎是最近出现的,LayerDrawable.setLayerGravity()表示
在 API 级别 23 中添加。
编辑:至于解决方案,这是我能想到的:
使用 9-patch 作为背景,具有透明的垂直可扩展部分
用另一个形状覆盖渐变中不需要的部分,其颜色与背景相同(显然需要是不透明的)
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/my_gradient" /> <item android:bottom="2dp"> <shape> <solid android:color="background color" /> </shape> </item> </layer-list>
使用
View
来保存渐变。根据EditText
在布局中的使用方式,可能还需要添加Framelayout
,例如:<FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:layout_width="match_parent" android:layout_height="2dp" android:layout_gravity="bottom" android:background="@drawable/my_gradient" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Some text" /> </FrameLayout>
关于Android:可绘制项目的重力属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35174465/