Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

android - How can I add gradient effect to background color of TextView in a ListView?

In reference to these questions :

Adding gradient effect to TextView in a ListView generates NPE

and

How to change color and font on ListView

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

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.

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

question from:https://stackoverflow.com/questions/14364617/how-can-i-add-gradient-effect-to-background-color-of-textview-in-a-listview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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>

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...