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的背景。单击查看如何使用它。您可以通过代码生成渐变并使用它