【发布时间】:2021-01-13 00:33:50
【问题描述】:
我正在尝试使用 android,我正在尝试在 drawable 中设置带有渐变文件的按钮的背景。
渐变文件:
<?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 中的值并将原色设置为 null。
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,则其他按钮会失去颜色。如果我不将其设置为 null,则不会应用我的 gradient_file。我只想解决这个问题。
【问题讨论】:
标签: android material-design android-styles