将backgroundtint应用于API 19的背景drawable

ema*_*272 16 android tint backwards-compatibility drawable

背景色调在API 23上正确应用,但在API 19上未正确应用.如何获得API 19及更低版本的drawable着色?

                    <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" />
Run Code Online (Sandbox Code Playgroud)

当然,我的Activity扩展了AppCompatActivity.

Dor*_*ean 28

这对我在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"
    />
Run Code Online (Sandbox Code Playgroud)

款式

<style name="Button" parent="Base.TextAppearance.AppCompat.Button" >
    <item name="backgroundTint">@color/fab_bg</item>
</style>
Run Code Online (Sandbox Code Playgroud)


Par*_*hat 6

我知道这个问题有点旧,但是您甚至不需要创建样式元素。

只需将支持库中的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" />
Run Code Online (Sandbox Code Playgroud)

  • 这应该是2019年公认的答案。更简单有效。 (2认同)

jon*_*nrz 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

更多信息:棒棒糖的 backgroundTint 对按钮没有影响

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

- 编辑 -

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