Android:theme.xml 文件中的值覆盖可绘制对象中的 gradient_file.xml
Android: theme.xml file in values overriding gradient_file.xml in drawable
我正在试验 android 并且我正在尝试在 drawable 中使用 gradient_file 设置按钮的背景。
gradient_file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
<gradient
android:startColor="@color/white"
android:endColor="@color/black"
android:angle="45"
android:type="linear" />
</shape>
按钮:
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@drawable/gradient_file"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.243" />
现在按钮的颜色不会改变,直到我手动转到 theme.xml 值并将原色设置为空。
theme.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyNewUIDesign" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@null</item> <!-- This was set to @color/purple_500 but if i dont make it null my gradient_file is not applied -->
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
现在唯一的问题是所有按钮都使用该颜色作为默认颜色,如果我将其设置为空,其他按钮将失去颜色。如果我不将其设置为 null,则不会应用 gradient_file。我只是想要一个解决方案。
您只需要将 app:backgroundTint="@null"
添加到按钮即可避免 colorPrimary
的阴影
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@drawable/gradient_file"
android:text="Button"
app:backgroundTint="@null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.243" />
我正在试验 android 并且我正在尝试在 drawable 中使用 gradient_file 设置按钮的背景。
gradient_file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
<gradient
android:startColor="@color/white"
android:endColor="@color/black"
android:angle="45"
android:type="linear" />
</shape>
按钮:
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@drawable/gradient_file"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.243" />
现在按钮的颜色不会改变,直到我手动转到 theme.xml 值并将原色设置为空。
theme.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyNewUIDesign" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@null</item> <!-- This was set to @color/purple_500 but if i dont make it null my gradient_file is not applied -->
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
现在唯一的问题是所有按钮都使用该颜色作为默认颜色,如果我将其设置为空,其他按钮将失去颜色。如果我不将其设置为 null,则不会应用 gradient_file。我只是想要一个解决方案。
您只需要将 app:backgroundTint="@null"
添加到按钮即可避免 colorPrimary
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@drawable/gradient_file"
android:text="Button"
app:backgroundTint="@null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.243" />