设置视图的背景色时,是否有办法获得渐变效果或半透明效果?例如,此代码:Android如何在setBackgroundColor上获得渐变效果
selView.setBackgroundColor(Color.rgb(240, 128, 128)); // light red
在列表视图中突出显示所选视图。无论是渐变还是背景都会很酷。
设置视图的背景色时,是否有办法获得渐变效果或半透明效果?例如,此代码:Android如何在setBackgroundColor上获得渐变效果
selView.setBackgroundColor(Color.rgb(240, 128, 128)); // light red
在列表视图中突出显示所选视图。无论是渐变还是背景都会很酷。
您首先需要在xml中创建渐变。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF00FF"
android:endColor="#FFFFFFFF"
android:angle="270"/>
</shape>
您应该将其添加到其中一个drawables.xml中。然后,您应该能够将此应用于xml中的ListView。
android:[email protected]/yourdrawable
或
setBackgroundResource(R.drawable.yourdrawable);
setBackgroundDrawable(R.drawable.my_fancy_gradient_or_picture)
我会给你一个示例代码
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#ff5500"
android:endColor="#999999"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#999999" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#ff5500"
android:startColor="#999999"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#999999" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape
android:shape="rectangle">
<gradient android:type="radial" android:gradientRadius="50"
android:startColor="#999999" android:endColor="#000000" />
<!-- <corners-->
<!-- android:radius="10dp" />-->
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
</item>
</selector>
从绘制
感谢sree的代码 – DroidBot 2012-01-06 09:28:19
感谢您的回答将此到您的视图。我无法使用它,因为我的应用程序的颜色变得太暗了。我不确定它会起作用,因为即使颜色发生变化,它也显得相同。 – 2011-05-23 20:42:53
您是否尝试在更改渐变定义后重建/清理项目? – 2011-05-23 21:18:21
好的,问题是我需要使用setBackgroundResource - 现在它像一个魅力。谢谢! – 2011-05-23 23:47:56