Android 棒棒糖';的背景色对按钮没有影响

Android 棒棒糖';的背景色对按钮没有影响,android,android-layout,tint,Android,Android Layout,Tint,我的活动中有一个按钮,我希望它有我的主题的重音颜色。 当然,我不想像棒棒糖制作前那样制作我自己的绘图,我想使用新的backgroundTint属性 <Button android:id="@+id/btnAddCode" android:layout_width="match_parent" android:layout_height="wrap_content" android:backgroundTint="@color/accent" andr

我的活动中有一个按钮,我希望它有我的主题的重音颜色。 当然,我不想像棒棒糖制作前那样制作我自己的绘图,我想使用新的
backgroundTint
属性

<Button
    android:id="@+id/btnAddCode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/accent"
    android:text="@string/addressInfo_edit_addCode" />
为什么我的色调被忽略?

编辑: 我只是想澄清一下,我确实在测试棒棒糖装置。
其他窗口小部件(如EditText)正确且自动着色。

因为属性
backgroundTint
仅用于API级别21及更高版本

我认为您需要设置
android:backgroundTint
以使
android:backgroundTint
工作


更准确地说,我的猜测是,您不能从材质主题中选择默认按钮背景,它被定义为
RippleDrawable

似乎给ripple drawable着色是没有意义的(按钮的默认背景是ripple drawable)

事实上,在查看了平台的默认按钮drawable之后,我找到了实现这一点的“正确”方法:。您必须在主题中定义这一点:

    <item name="android:colorButtonNormal">@color/accent</item>
@颜色/重音
(当然,这只适用于21+级。)

警告:由于这是在主题中定义的,因此所有按钮(至少是使用该主题的活动中的所有按钮)都将使用给定的颜色

另外,您还可以通过定义以下内容来更改涟漪颜色:

    <item name="android:colorControlHighlight">@color/accent_ripple</item>
@color/accent\u波纹
坏消息 正如BoD所说,在棒棒糖5.0(API级别21)中为按钮的背景着色是毫无意义的

好消息 棒棒糖5.1(API级别22)似乎通过更改btn_mtrl_default_shape.xml(以及其他文件)解决了这一问题:

好消息 新的支持库(版本22.1+)支持许多组件,包括

不幸的是,
android:backgroundTint
属性仍然不起作用(可能我做错了)——因此您必须在代码中使用设置
ColorStateList
。很高兴看到android:backgroundTint在未来得到支持更新:Marcio Granzotto评论说,
app:backgroundTint
在AppCompatButton上工作!请注意,它是
app:
,而不是
android:
,因为它在app/库中

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <AppCompatButton
        android:id="@+id/mybutton"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Testing, testing"
        app:backgroundTint="#ff00ff"/>

</LinearLayout>
当然,您应该从颜色资源中获取
ColorStateList
,但我很懒,所以

哦,别忘了把你的应用程序主题建立在一个
主题上。AppCompat
主题,否则compat视图会非常非常悲伤…;)


这在2.3.7(姜饼MR1)和5.0(棒棒糖“经典”)上都起到了作用。

为了解决Android 5.0.x上的着色问题,我使用了如下方法:

public static void setButtonTint(Button button, ColorStateList tint) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && button instanceof AppCompatButton) {
        ((AppCompatButton) button).setSupportBackgroundTintList(tint);
    } else {
        ViewCompat.setBackgroundTintList(button, tint);
    }
}

它仅对API 21使用支持方法,对所有其他情况使用ViewCompat方法。

我通常使用PorterDuff动态执行此操作:

mbutton = (Button) findViewById(R.id.mybutton);
mbutton.getBackground().setColorFilter(anycolor, PorterDuff.Mode.MULTIPLY);

你可以查看不同的混合模式和很好的示例。

如果我们查看支持库的源代码,我们会发现它通常是着色的,这是已知的按钮,但是如果我们更改按钮的形状(我有圆形按钮),着色在api中无法正常工作谷歌上报道了类似的问题

但在Android支持库发布后,修订版(2016年3月)解决了此缺陷。

问题:FloatingActionButton.setBackgroundTintList(@Nullable ColorStateList tint)不再更改背景色

将支持库更新为
Android支持库至23.2.1

使用设计支持库(23.2.1)appcompatwidgets如下

AppCompat(又名ActionBarCompat)最初是作为 Android 4.0 ActionBar API,用于运行在姜饼上的设备, 在后端口实现之上提供公共API层 以及框架的实施。AppCompat v21提供了一个API和 Android 5.0的最新功能集


在使用AppCompat时自动为小部件着色的功能是 非常有助于保持强大的品牌和一致性 在整个应用程序中。膨胀布局时会自动执行此操作 -将按钮替换为AppCompatButton,将TextView替换为AppCompatTextView等,以确保每个按钮都支持着色。在里面 在这个版本中,那些支持色调的小部件现在可以公开使用, 允许您保持着色支持,即使您需要一个子类 支持的小部件的名称


请注意,recyclerview最新更新的库也可能导致此错误

此命令

  sendBtnView.setBackgroundTintList(colorState)
过去工作得很好,但别再为我工作了。经过研究发现,原因是添加到gradle依赖项中的库:

  compile 'com.android.support:recyclerview-v7:+'
因此,我尝试将其更改为23.02.1,正如Amit Vaghela post中推荐的那样。 我改成

  compile  'com.android.support:recyclerview-v7:23.02.1'
但是gradle error说recyclerview库没有这个版本(23.02.1)(gradle在Jcenter raw.github或repo中找不到)

然后,因为我知道setBackgroundTintList命令在过去的版本22.02.0中在gradle dependencies中的所有其他lib中都很有效。 所以我把它改成:

compile   'com.android.support:recyclerview-v7:22.02.0'

现在它又开始工作了。

只需使用
app:backgroundTint
而不是
android:backgroundTint
,该色调将在棒棒糖下方生效。原因是
AppCompatActivity
使用
AppCompatViewInflater
将按钮或文本视图自动更改为AppCompatButton或AppCompatTextView,然后
app:backgroundTint
生效


我不确定是否建议这样做,但您可以试试:

Drawable newDrawable = mBtnAction.getBackground();  // obtain Bg drawable from the button as a new drawable
DrawableCompat.setTint(newDrawable, mActivity.getHomeTobBarBGColor());  //set it's tint
mBtnAction.setBackground(newDrawable);  //apply back to button

在一般意义上,它是有效的。已尝试使用ViewCompat,但似乎无法正常工作。

只需使用app:backgroundTint,而不用android:backgroundTint,在API 19到API 27上测试即可

<?xml version="1.0" encoding="utf-8"?>
  <android.support.v7.widget.AppCompatButton 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/retry"
    android:textColor="@android:color/white"
    app:backgroundTint="@android:color/holo_red_dark" />


如果您使用的是androidx,则可以使用
backgroundTint

compile   'com.android.support:recyclerview-v7:22.02.0'
Drawable newDrawable = mBtnAction.getBackground();  // obtain Bg drawable from the button as a new drawable
DrawableCompat.setTint(newDrawable, mActivity.getHomeTobBarBGColor());  //set it's tint
mBtnAction.setBackground(newDrawable);  //apply back to button
<?xml version="1.0" encoding="utf-8"?>
  <android.support.v7.widget.AppCompatButton 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/retry"
    android:textColor="@android:color/white"
    app:backgroundTint="@android:color/holo_red_dark" />
<style name="Button_Primary">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:backgroundTint">@color/button_primary_selector</item>
    <item name="backgroundTint">@color/button_primary_selector</item><!--needed for android 5-->
</style>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:local="http://schemas.android.com/apk/res-auto">
    <item android:state_enabled="true" android:color="@android:color/holo_blue_dark" />
    <item android:state_enabled="false" android:color="@android:color/darker_gray" />
</selector>
<Button style="@style/Button_Primary"/>