Android:渐变编辑框的技巧

4
我想知道以编程方式淡出编辑文本字段的最佳方法是什么?
例如,我希望编辑文本字段看起来像这样: enter image description here 目前,我只是使用静态图像,并使用gimp将其淡出,但我希望将它们全部更改为淡化的编辑文本字段。
非常感谢您的任何建议!
2个回答

2

您可以通过渐变来实现类似的效果。

首先在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" />

如果想了解关于形状可绘制对象和渐变的更多信息,请参考此处


好的,谢谢。这是一个很好的开始。我目前正在使用android:background皮肤化每个编辑文本框。我如何在已皮肤化的编辑文本框上实现淡入淡出效果?希望我的问题对你有意义,哈哈。 - sean9898
你能发布一个包含“skinned” EditText之一的简单XML吗? - Leszek
<EditText android:id="@+id/skinnedbox" android:layout_width="130dp" android:layout_height="50dp" android:background="@drawable/myskin" /> - sean9898
我不确定。尝试将EditText包装到布局中,并为布局使用皮肤和为EditText使用形状,或者将shape与其他xml属性一起使用,如android:drawableTopandroid:drawableBottom等。 - Leszek

1

我相信有一个更简单的答案,并且它已经内置在控件中。由于图片尺寸非常小,很难确定这是否正是您想要的。

当我试图找到有关淡化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);  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接