我創建了一個名為 .xml 的 xml 檔案round_button。這是一個帶有漸變的簡單圓形按鈕。這是簡單的代碼
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="3000dip" />
<gradient android:type="linear" android:startColor="#8dbab3" android:endColor="#0DCAAC" />
</shape>
這就是輸出:

這是我的主要活動的 xml 檔案的代碼。
<Button
android:id="@ id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@ id/city"
android:text="Search"
android:background="@drawable/round_button"
/>
但輸出是這個:
為什么它在形狀正確的情況下確實有漸變?
uj5u.com熱心網友回復:
您的代碼中的所有內容都可以,只需將Button更改為androidx.appcompat.widget.AppCompatButton,如下所示,您將能夠實作您想要的結果。
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@ id/city"
android:text="Search"
android:background="@drawable/round_button"
/>
uj5u.com熱心網友回復:
轉到themes.xml 并更改父級。parent=Theme.MaterialComponents.NoActionBar.Bridge
<style name="Theme.YourApp" parent="Theme.MaterialComponents.NoActionBar.Bridge">
//yourcode
</style>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/472084.html
