如何在 ListView 中为 TextView 的背景颜色添加渐变效果? [英] How can I add gradient effect to background color of TextView in a ListView?

查看:35
本文介绍了如何在 ListView 中为 TextView 的背景颜色添加渐变效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这些问题:

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

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

我想知道如何在 ListView 中设置 TextView 的背景并带有渐变效果?

I would like to know how to go about setting the background of a TextView in a ListView with gradient effect?

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

In one of the questions above, I ended up having the gradient effect added to the text in the TextView. And after skimming through the second question, it seems I can add only fixed background colors.

如何为背景添加渐变?我应该制作一个 CustomListAdapter 吗?

How do I go about adding gradient to background? Should I make a CustomListAdapter?

推荐答案

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

You just need to create a drawable resource (see an example below), and add it to the layout you created for your ListItem.

drawable(在您的 resdrawable 文件夹中 - 随便命名 - listgrad.xml for ex)可能如下所示:

The drawable (in your resdrawable folder - name it whatever - listgrad.xml for ex) could look like:

<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>

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

The you would add it to the layout for your list item (the layout.xml file you define for this) like this code snippet:

<TextView
        android:id="@+id/ranking_order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/list_grad"
        />
...

这篇关于如何在 ListView 中为 TextView 的背景颜色添加渐变效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆