4

我想知道以编程方式淡出编辑文本字段的最佳方法是什么?

例如,我希望编辑文本字段看起来像这样:

在此处输入图像描述

目前我只是使用我使用 gimp 淡出的静态图像,但我会将它们全部更改为淡入淡出的编辑文本字段。

非常感谢任何输入!

4

2 回答 2

2

您可以使用渐变获得类似的结果。

首先在drawable forlder xml文件中创建shape元素,例如:

<!-- 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" />

有关形状可绘制和渐变的更多信息,请查看此处

于 2012-08-01T16:04:58.087 回答
1

我相信对此有一个更简单的答案,并且它内置在控件中。由于图片尺寸极小,很难判断这是否正是您想要的。

我在尝试查找有关淡化 EditTexts 的更多信息时发现了这一点,所以我想我会分享我已经知道的东西,因为接受的答案非常复杂

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);  
于 2014-09-20T19:45:40.440 回答