我想在我用 Kotlin 開發的 Android 應用程式中制作一個漸變按鈕。我已經為漸變背景制作了可繪制檔案,但是當我為按鈕提供自定義背景時,它不起作用,沒有任何變化。這是漸變背景的代碼,下面是按鈕的 XML 代碼。
<item>
<shape android:shape="rectangle">
<gradient android:startColor="@color/start_color"
android:endColor="@color/end_color"/>
<corners android:radius="10dp"/>
</shape>
</item>
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="55dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@ id/logo"
android:layout_marginHorizontal="26dp"
tools:layout_editor_absoluteX="16dp"
app:cornerRadius="10dp"
android:textSize="20sp"
android:text="Create an account"
android:background="@drawable/create_acc_btn"
android:fontFamily="@font/montserrat" />
uj5u.com熱心網友回復:
你只需要修復你的漸變可繪制檔案。
從中洗掉 item 標簽并像這樣放置 android xml 命名空間宣告:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="@color/start_color"
android:endColor="@color/end_color"/>
<corners android:radius="10dp"/>
</shape>
并將這一行添加到您的按鈕 xml 代碼中:
app:backgroundTint="@null"
沒有這個,你的漸變背景就不會出現
注意: MaterialButton 管理自己的背景來控制高度、形狀、顏色和狀態。考慮使用 backgroundTint、shapeAppearance 和其他可用的屬性。自定義背景將忽略這些屬性,您應該考慮處理互動狀態,例如按下、聚焦和禁用
使用自定義背景時,您也會失去漣漪效應。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/388666.html
下一篇:一鍵多行程
