2015-12-12 97 views
2

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> 

app running on API19 app running on API21

+0

您background_selector缺少標籤,如果它的錯字或錯過了請更新N檢查...我已經檢查了一切,它應該工作沒有你提到的錯誤。檢查你的顏色或風格以上api v-21 – Shishram

+0

謝謝你的觀察,因爲實際上我很好地忘記了值-v21,所以它基本上是空的。我剛剛試圖複製和過去值爲v21的值,但仍然存在相同的問題。任何其他建議? –

+0

@Shishram,我已經完全用獨立測試重新提出了這個問題,請你再看一遍嗎? –

回答

0

你沒有牛逼o覆蓋onTouch()方法

只需嘗試將選擇器xml文件放到按鈕的背景。

<Button 
    android:id="@+id/id_Login_login_btn" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:background="@drawable/your_selector_xmlfile" 
    android:text="Login" /> 
+0

感謝提示,但我確實需要重寫onTouch(),因爲我需要知道用戶是否按下了任意鍵來播放給定的聲音。和他們當我重寫OnTouch()它沒有設置按下狀態我的ImageButtons。 –

+0

是的,但如果你想獲得按鈕按下事件onClick()會很好,而不是onTouch() 嘗試添加onclickListners到你的按鈕 因爲你有一個活動中有這麼多的按鈕,你最好讓你的活動實現OnClickListner觀察點擊按鈕 例如[請參閱此處](https://anujarosha.wordpress.com/2011/11/13/how-to-implements-onclicklistener-for-a-view-item-in-android/) – Jayanth

0

解決了!對我來說,這個xml屬性android:gradientRadius在版本API21上有一個bug。

我解決這個問題的方法是創建一個drawable-v21文件夾並複製所有具有此xml屬性的xml drawable,並使用本附錄之一:px(像素),dp(密度無關像素),sp (基於首選字體大小的縮放像素),單位爲英寸,毫米(毫米)。

雖然保留默認的可繪製文件夾沒有附錄。

否則我無法使它工作,當它在API21上工作時,它並沒有在所有其他API上工作,而當它在所有其他API上工作時,它在API21上沒有工作。

對我來說,它是這樣工作的。

例如:

繪製

android:gradientRadius="110" 

繪製-V21

的<選擇
android:gradientRadius="55%p" 
相關問題