关于这些问题:
在ListView中为TextView添加渐变效果生成NPE
如何更改ListView上的颜色和字体
我想知道如何在 ListView
的 TextView
上设置带有渐变效果的背景?
在上面的其中一个问题中,我最终为TextView
中的文本添加了渐变效果。浏览第二个问题后,似乎我只能添加固定的背景颜色。
我该如何添加渐变背景?
我应该制作一个 CustomListAdapter
吗?
关于这些问题:
在ListView中为TextView添加渐变效果生成NPE
如何更改ListView上的颜色和字体
我想知道如何在 ListView
的 TextView
上设置带有渐变效果的背景?
在上面的其中一个问题中,我最终为TextView
中的文本添加了渐变效果。浏览第二个问题后,似乎我只能添加固定的背景颜色。
我该如何添加渐变背景?
我应该制作一个 CustomListAdapter
吗?
<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文件),代码片段如下所示:<TextView
android:id="@+id/ranking_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/list_grad"
/>
...
创建了渐变后,你可以将其应用于几乎任何东西,无论是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>
<?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>
创建完这个文件后,只需通过以下方式之一设置背景:
通过代码:listView.setBackgroundResource(R.drawable.customshape);
通过XML,只需将以下属性添加到容器(例如LinearLayout或任何字段)中:
android:background="@drawable/customshape"