Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.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 如何将动态值传递给渐变xml_Android_Xml_Gradient - Fatal编程技术网

Android 如何将动态值传递给渐变xml

Android 如何将动态值传递给渐变xml,android,xml,gradient,Android,Xml,Gradient,我有以下情况:在我的活动中有一个渐变背景: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:angle="90" android:endColor="#0F7D71" android:startColor="#0E

我有以下情况:在我的活动中有一个渐变背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="90"
        android:endColor="#0F7D71"
        android:startColor="#0E725B"
        android:type="linear" />
</shape>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_gradient_background"
    tools:context=".ui.login.LoginActivity">

以下是我如何设置活动背景的方法:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="90"
        android:endColor="#0F7D71"
        android:startColor="#0E725B"
        android:type="linear" />
</shape>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_gradient_background"
    tools:context=".ui.login.LoginActivity">


但我也需要在一些其他颜色的屏幕上使用渐变背景。现在我为每个屏幕创建一个新的渐变文件,但我认为这不是最好的方法。所以,您能帮我解释一下如何将代码中的颜色传递给项目中只有一个渐变xml的颜色吗。提前谢谢

根本不需要使用XML文件,您应该从代码创建一个
GradientDrawable
,并根据每个屏幕对其进行自定义,然后将其设置为活动或片段的根XML的背景。单击查看如何使用它。

您可以通过代码生成渐变并使用它