将backgroundTint应用于API 19的背景可绘制对象。

19

在API 23上正确应用了backgroundTint,但在API 19上未能应用。我该如何使得API 19及以下版本的可绘制物品着色?

                    <Button
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:id="@+id/AbResetBtn"
                    android:background="@android:drawable/stat_notify_sync"
                    android:backgroundTint="@color/button_material_light" />

当然,我的Activity扩展了AppCompatActivity

4个回答

36

这对我在API19设备上,支持lib v7的情况下有效。

布局

<Button
    android:id="@id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/label"
    style="@style/Button"
    />
<style name="Button" parent="Base.TextAppearance.AppCompat.Button" >
    <item name="backgroundTint">@color/fab_bg</item>
</style>

1
这是一个比被接受的答案更优雅的解决方案 - 它避免了需要通过编程设置颜色,不需要直接引用AppCompatButton,并将所有必需的布局代码保留在样式和其他XML文件中。整洁而有序。 - BasicPleasureModel
你是对的。我尝试了这个更新的解决方案,它也有效。 - ema3272

9
我知道这是一个旧问题,但你甚至不需要创建样式元素。只需使用来自支持库的AppCompatButton和app:命名空间即可。
<android.support.v7.widget.AppCompatButton android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:id="@+id/AbResetBtn"
                    android:background="@android:drawable/stat_notify_sync"
                    app:backgroundTint="@color/button_material_light" />

2
这应该是2019年的最佳答案。更简单和有效。 - xarlymg89

5
您需要使用Android支持库22.1+来使用AppCompatButton http://android-developers.blogspot.se/2015/04/android-support-library-221.html 但遗憾的是,您无法在XML中实现此操作。
在活动的onCreate方法中,执行以下操作:
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AppCompatButton v = (AppCompatButton) findViewById(R.id.mybutton);
        ColorStateList csl = new ColorStateList(new int[][]{new int[0]}, new int[]{0xffffcc00});
        v.setSupportBackgroundTintList(csl);
    }
}

更多信息请查看:Lollipop的backgroundTint对按钮没有影响

提示:也许你可以在xml中使用app:backgroundTint="@color/button_material_light"来完成所有操作,但我没有测试过。

--编辑--

请查看@ema3272的第二条评论以获取完整解决方案


我使用Support Library 23.1.1。实际上,这个按钮包含在一个Fragment中的视图中(它是一个“帮助”屏幕)。我已经尝试在Fragment类的“onCreateView”中使用上述代码。然而,我得到了一个编译错误,说“意外转换为AppCompatButton:布局标记是Button”。顺便说一下,在Android Studio预览“帮助”屏幕时,问题已经存在。上面的XML提示也不起作用。 - ema3272
1
@ema3272 在你的xml中也将其更改为AppCompatButton - jonathanrz
好的。这解决了问题。但是,仅将 Button 转换为 AppCompatButton 是不够的,您必须在 XML 中使用 android.support.v7.widget.AppCompatButton,并使用上述代码才能获得正确的结果。官方文档 http://developer.android.com/intl/pt-br/reference/android/support/v7/widget/AppCompatButton.html 上说:“当您在布局中使用 Button 时,将自动使用此(类)。编写自定义视图时,只需要手动使用此类”,这是不正确的。 - ema3272

0

你需要将一个“Button”更新为“androidx.appcompat.widget.AppCompatButton”,并将“android:backgroundTint”更新为“app:androidTint”

之前:

 <Button
                android:id="@+id/button"
                android:layout_width="200dp"
                android:layout_height="0dp"
                android:textColor="@color/colorAccent"
                android:backgroundTint="@color/colorAccent"
                android:background="@drawable/empty_list_state_button"
                android:text="@string/button_title"
                app:layout_constraintTop_toBottomOf="@id/distance"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                />

之后:

 <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/button"
                android:layout_width="200dp"
                android:layout_height="0dp"
                android:textColor="@color/colorAccent"
                app:backgroundTint="@color/colorAccent"
                android:background="@drawable/empty_list_state_button"
                android:text="@string/button_title"
                app:layout_constraintTop_toBottomOf="@id/distance"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接