Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中将布局视图的背景色设置为渐变色?_Android_Android Layout - Fatal编程技术网

在Android中将布局视图的背景色设置为渐变色?

在Android中将布局视图的背景色设置为渐变色?,android,android-layout,Android,Android Layout,如何指定Android布局视图元素的背景“颜色”应为渐变(以特定角度) 我希望在XML中指定这一点,即不在运行时指定。最好是作为一种样式,我可以使用style属性应用于我希望的任何布局?您可以使用如下内容: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle

如何指定Android布局视图元素的背景“颜色”应为渐变(以特定角度)


我希望在XML中指定这一点,即不在运行时指定。最好是作为一种样式,我可以使用
style
属性应用于我希望的任何布局?

您可以使用如下内容:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    <gradient android:startColor="#A1A1A1" 
              android:centerColor="#BDBDBD"
              android:endColor="#A4A4A4" 
              android:angle="-90" />
</shape>


创建渐变(选择喜欢的颜色)。把它放在drawable中,瞧,你有了自己的形状作为背景:
android:background=“@drawable/your\u xml的\u name\u”

这就是我设置渐变样式的方式。希望这有帮助。但我已经把它用于textview了。也许你必须做出一些改变来适应你的布局背景

            Shader textShader = new LinearGradient(0, 0, 0, 20, new int[] {
            Color.WHITE, getResources().getColor(//some color),
            getResources().getColor(//some color), Color.WHITE },
            new float[] {  0.25f,0.50f,0.75f, 1 }, TileMode.CLAMP);
            textview.getPaint().setShader(textShader);

/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="#FFFFFF"
        android:endColor="#00000000"
        android:angle="45"/>    
</shape>

您可以通过替换
android:angle
值来指定角度,通过替换
android:startColor
android:endColor

来指定开始/结束颜色。尽管他在问题中提到他希望有一个xml解决方案,而不是运行时解决方案,但我们可以在运行时更改xml渐变值吗
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/gradient"
    >   
</LinearLayout>