2017-10-07 120 views
0

此問題已存在於此處。但我想逐步瞭解這一點。 我在依賴關係中粘貼了gradle中的依賴關係。drawableTint無法正常使用AppCompact-v7:25.3.1

compile 'com.android.support:appcompat-v7:25.3.1' 

而不是做什麼。 我的XML代碼是在這裏

<Button 
    android:id="@+id/button_not_working" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/imageViewGroup" 
    android:layout_marginTop="60dp" 
    android:paddingEnd="9dp" 
    android:layout_alignStart="@+id/imageViewGroup" 
    android:layout_alignEnd="@+id/imageViewGroup" 
    android:background="@drawable/button_background" 
    android:text="Drawable Tine not working" 
    android:textColor="@color/white" 
    android:textSize="@dimen/font_size_small" 
    android:textAlignment="center" 
    android:layout_marginBottom="10dp" 
    android:drawableEnd="@drawable/forword_arrow" 
    android:drawableTint="@color/white" 
    android:gravity="center" 
    /> 

這是圖片

enter image description here

的forward_arrow的顏色沒有改變。 這裏有幾個答案,但想知道如何一步一步做。 就是這樣。

+2

可能重複的[繪製着色的API <21](https://stackoverflow.com/questions/29155463/drawable-tinting-for-api-21) –

+0

是的,我看到了答案,但你能幫我理解這一步一步?我是否可以在沒有.java文件的幫助下僅通過xml文件來處理這個問題? – Anna

+0

@DimaKozhevin這是關於在TextView *中着色*複合可繪製的。這與圖像可繪製着色無關。 –

回答

-1

您必須使用tint atttibute

編輯:我的錯誤,它應該是backgroundTint

+0

'tint'用於ImageView和ImageButton的前景圖像。 'backgroundTint'用於背景。 'drawableTint'用於TextView或Button的複合drawable。此外,'app:tint'和'android:tint'之間有區別(對於其他屬性也是如此)。 'app:tint'自支持庫25.4.0以來按預期工作。 –

0

在TextView中(和他們的後裔如Button)着色化合物可繪製在API 23介紹(安卓6)通過android:drawableTintandroid:drawableTintMode屬性。

截至今天,該功能尚未被移植到AppCompat支持庫。


當然,我試圖自己解決這個問題。查看appcompat-extra庫。它有XpAppCompatTextViewXpAppCompatButton小部件。您的代碼可能會是這樣:

<android.support.v7.widget.XpAppCompatButton 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Drawable tint working" 
    android:drawablePadding="8dp" 
    app:drawableEnd="@drawable/forword_arrow" 
    app:drawableTint="?android:textColorPrimary"/> 

注意android:drawablePadding仍然使用android前綴。

此小工具的佈局預覽可能會中斷。在這種情況下,添加以下代碼,所以你至少知道結果的應用程序將是什麼樣子:

tools:drawableEnd="@drawable/forword_arrow" 
    tools:drawableTint="?android:textColorPrimary" 

獲得庫把這個你應用模塊中的build.gradle:

repositories { 
    maven { url 'https://dl.bintray.com/consp1racy/maven/' } 
} 

dependencies {  
    compile 'net.xpece.android:appcompat-extra:1.2.0' 
}