如何在ListView中为TextView的背景颜色添加渐变效果?

Kaz*_*ara 35 android gradient textview

参考这些问题:

在ListView中向TextView添加渐变效果会生成NPE

如何在ListView上更改颜色和字体

我想知道如何去设置的背景TextViewListView有渐变效果?

在上面的一个问题中,我最终将渐变效果添加到了文本中TextView.浏览第二个问题后,我似乎只能添加固定的背景颜色.

如何向背景添加渐变?我应该做一个CustomListAdapter

Boo*_*ger 86

您只需创建一个可绘制资源(请参阅下面的示例),并将其添加到您为ListItem创建的布局中.

drawable(在res\drawable文件夹中 - 无论如何命名 - listgrad.xml for ex)可能如下所示:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
      android:startColor="@color/gradient_start"
      android:endColor="@color/gradient_end"
      android:angle="-270" /> 
</shape>
Run Code Online (Sandbox Code Playgroud)

您可以将它添加到列表项的布局(您为此定义的layout.xml文件),如下代码片段:

<TextView
        android:id="@+id/ranking_order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/list_grad"
        />
...
Run Code Online (Sandbox Code Playgroud)


Aji*_*ngh 9

创建渐变后,您几乎可以将其应用于任何内容,无论是 textView、布局还是按钮。

要了解如何创建和使用渐变,请参阅此链接

要创建渐变,您需要将其添加到以下目录

在此处输入图片说明

渐变代码将是这样的 -

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:startColor="#ff2d9a59"
                android:centerColor="#ff42959a"
                android:endColor="#ff23729a"
                android:angle="135"/>
        </shape>
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)