EDITEDXML繪製不上正常工作棒棒糖API21
決定刪除以前所有的問題,因爲我做了一個獨立的應用程序只是爲了解決這一問題。
問題:當定義爲Button的背景時,xml drawable在API21上無法正常工作。並適用於所有其他API。包括仍然是棒棒糖的API22。真的很奇怪..
做了這個獨立的應用程序後,我意識到它可能真的是一個API21錯誤相關的xml漸變。所以在這個應用程序中,我只放了一個按鈕,其中我將背景設置爲一個可繪製的xml選擇器,該選擇器根據按鈕的狀態更改背景可繪製文件。
從我能理解的是,API30上的它只使用漸變的開始顏色。
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<Button
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/imageView"
android:background="@drawable/key_pressed_selector"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
background_key_default.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/grey"
android:endColor="@color/grey"
/>
<corners android:radius="15dp"/>
background_yellow.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="radial"
android:gradientRadius="100"
android:startColor="@color/dark_blue"
android:endColor="@color/yellow"
/>
<corners android:radius="15dp"/>
</shape>
background_pressed_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background_yellow"
android:state_pressed="true"/>
<item android:drawable="@drawable/background_key_default"/>
</selector>
RES /價值/ colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#ffff39</color>
<color name="dark_blue">#332ff2</color>
<color name="grey">#b1b2b6</color>
</resources>
您background_selector缺少標籤,如果它的錯字或錯過了請更新N檢查...我已經檢查了一切,它應該工作沒有你提到的錯誤。檢查你的顏色或風格以上api v-21 – Shishram
謝謝你的觀察,因爲實際上我很好地忘記了值-v21,所以它基本上是空的。我剛剛試圖複製和過去值爲v21的值,但仍然存在相同的問題。任何其他建議? –
@Shishram,我已經完全用獨立測試重新提出了這個問題,請你再看一遍嗎? –