我想知道以编程方式淡出编辑文本字段的最佳方法是什么?
例如,我希望编辑文本字段看起来像这样:
目前,我只是使用静态图像,并使用gimp将其淡出,但我希望将它们全部更改为淡化的编辑文本字段。
非常感谢您的任何建议!
例如,我希望编辑文本字段看起来像这样:
![enter image description here](https://istack.dev59.com/stabX.webp)
非常感谢您的任何建议!
您可以通过渐变来实现类似的效果。
首先在drawable文件夹中创建一个带有shape
元素的XML文件,例如:
<!-- mygradient.xml -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/black" />
<gradient
android:angle="90"
android:centerColor="@color/grey"
android:endColor="@color/white"
android:startColor="@color/black"
android:type="linear" />
</shape>
接下来,将您的xml用作某些视图的背景:
<TextView
android:background="@drawable/mygradient"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
如果想了解关于形状可绘制对象和渐变的更多信息,请参考此处
。我相信有一个更简单的答案,并且它已经内置在控件中。由于图片尺寸非常小,很难确定这是否正是您想要的。
当我试图找到有关淡化EditText的更多信息时,我发现了这一点,因此我认为我应该分享我已经知道的事情,因为接受的答案非常复杂。
EditText textbox = (EditText) findViewById(R.id.textbox);
// This enables the fading automatically.
// Use Horizontal if you want the sides to fade!
textbox.setVerticalFadingEdgeEnabled(true);
// This allows you to change to depth of the fade
// Low values like 10 has an incredible small fade, but values like 100 seem to be really good!
textbox.setFadingEdgeLength(100);
EditText
包装到布局中,并为布局使用皮肤和为EditText
使用形状,或者将shape
与其他xml属性一起使用,如android:drawableTop
,android:drawableBottom
等。 - Leszek