将backgroundtint应用于API 19的背景drawable

时间:2015-12-05 16:39:28

标签: android drawable backwards-compatibility tint

背景色调在API 23上正确应用,但在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 个答案:

答案 0 :(得分:24)

这对我在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 :(得分:4)

您需要使用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's backgroundTint has no effect on a Button

提示:也许你可以使用app在xml中完成所有工作:backgroundTint =&#34; @ color / button_material_light&#34;,但我没有经过测试。

- 编辑 -

检查完整解决方案的@ ema3272第二条评论

答案 2 :(得分:3)

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

只需将支持库中的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" />

答案 3 :(得分:0)

您必须将“按钮”更新为“ 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"
                />