经过几天的摸索,终于找到不使用图片实现背景渐变的方法
首先建立文件drawable/shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#FFFFFFFF" android:endColor="#FFFF0000"
android:angle="270"/>
</shape>
在该文件中设置渐变的开始颜色(startColor)、结束颜色(endColor)和角度(angle)
接着创建一个主题values/style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NewTheme" parent="android:Theme">
<item name="android:background">@drawable/shape</item>
</style>
</resources>
然后在AndroidManifest.xml文件中的application或activity中引入该主题,如:
<activity android:name=".ShapeDemo" android:theme="@style/NewTheme">
该方法同样适用于控件
首先建立文件drawable/shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#FFFFFFFF" android:endColor="#FFFF0000"
android:angle="270"/>
</shape>
在该文件中设置渐变的开始颜色(startColor)、结束颜色(endColor)和角度(angle)
接着创建一个主题values/style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NewTheme" parent="android:Theme">
<item name="android:background">@drawable/shape</item>
</style>
</resources>
然后在AndroidManifest.xml文件中的application或activity中引入该主题,如:
<activity android:name=".ShapeDemo" android:theme="@style/NewTheme">
该方法同样适用于控件
本文介绍了一种不使用图片而通过代码实现背景渐变的方法。具体步骤包括:创建drawable/shape.xml文件来定义渐变的颜色及角度;接着在values/style.xml中创建主题并引用shape.xml;最后,在AndroidManifest.xml中应用此主题。
325

被折叠的 条评论
为什么被折叠?



