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

时间:2021-08-13 03:26:56

In reference to these questions :

参考这些问题:

Adding gradient effect to TextView in a ListView generates NPE

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

and

How to change color and font on ListView

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

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

我想知道如何在具有渐变效果的ListView中设置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.

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

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

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

3 个解决方案

#1


75  

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

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

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

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>

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

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

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

#2


4  

Once you create a gradient you can apply it to pretty much anything let it be textView, layout or button.

创建渐变后,您可以将其应用于几乎所有的文本视图,布局或按钮。

To understand how to create and use a gradient refer this link.

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

To create a gradient you need to add it to the below directory

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

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

Code for gradient would be something like this -

渐变代码将是这样的 -

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

#3


1  

Referred from here : How do I create a ListView with rounded corners in Android? (I have found it very useful.)

从这里引用:如何在Android中创建带圆角的ListView? (我发现它非常有用。)

Add the following into a file (say gradient.xml) and then place it in (res/drawable/gradient.xml) directory.

将以下内容添加到文件(例如gradient.xml)中,然后将其放在(res / drawable / gradient.xml)目录中。

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
     <gradient 
         android:startColor="#SomeGradientBeginColor"
         android:endColor="#SomeGradientEndColor" 
         android:angle="270"/> 

    <corners 
         android:bottomRightRadius="7dp" 
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp" 
         android:topRightRadius="7dp"/> 
</shape> 

Once you are done with creating this file,just set the background in one of the following ways:

完成创建此文件后,只需使用以下方法之一设置背景:

Through Code: listView.setBackgroundResource(R.drawable.customshape);

通过代码:listView.setBackgroundResource(R.drawable.customshape);

Through XML,just add the following attribute to the container (ex: LinearLayout or to any fields):

通过XML,只需将以下属性添加到容器中(例如:LinearLayout或任何字段):

android:background="@drawable/customshape"

#1


75  

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

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

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

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>

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

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

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

#2


4  

Once you create a gradient you can apply it to pretty much anything let it be textView, layout or button.

创建渐变后,您可以将其应用于几乎所有的文本视图,布局或按钮。

To understand how to create and use a gradient refer this link.

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

To create a gradient you need to add it to the below directory

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

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

Code for gradient would be something like this -

渐变代码将是这样的 -

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

#3


1  

Referred from here : How do I create a ListView with rounded corners in Android? (I have found it very useful.)

从这里引用:如何在Android中创建带圆角的ListView? (我发现它非常有用。)

Add the following into a file (say gradient.xml) and then place it in (res/drawable/gradient.xml) directory.

将以下内容添加到文件(例如gradient.xml)中,然后将其放在(res / drawable / gradient.xml)目录中。

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
     <gradient 
         android:startColor="#SomeGradientBeginColor"
         android:endColor="#SomeGradientEndColor" 
         android:angle="270"/> 

    <corners 
         android:bottomRightRadius="7dp" 
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp" 
         android:topRightRadius="7dp"/> 
</shape> 

Once you are done with creating this file,just set the background in one of the following ways:

完成创建此文件后,只需使用以下方法之一设置背景:

Through Code: listView.setBackgroundResource(R.drawable.customshape);

通过代码:listView.setBackgroundResource(R.drawable.customshape);

Through XML,just add the following attribute to the container (ex: LinearLayout or to any fields):

通过XML,只需将以下属性添加到容器中(例如:LinearLayout或任何字段):

android:background="@drawable/customshape"