Android Button drawableEnd Style Item

标签 android android-layout

我在 styles.xml 文件中定义了一个按钮样式。

<style name="Button" parent="Widget.AppCompat.Button.Borderless.Colored">
    <item name="android:drawablePadding">@dimen/padding_medium</item>
    <item name="android:paddingStart">@dimen/padding_medium</item>
    <item name="android:paddingEnd">@dimen/padding_medium</item>
    <item name="android:textAppearance">@style/ButtonTextAppearance</item>
</style>
<style name="Button.Secondary" parent="Button">
    <item name="android:background">@drawable/secondary_button_state</item>
    <item name="android:textColor">@color/blue</item>
</style>
<style name="Button.Secondary.Large" parent="Button.Secondary">
    <item name="android:drawableEnd">@drawable/ic_chevron</item>
    <item name="android:drawableTint">?colorPrimary</item>
    <item name="android:gravity">center|start</item>
</style>

我在 View 中使用它

<androidx.appcompat.widget.AppCompatButton
   android:id="@+id/action_date_range"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   style="@style/Button.Secondary.Large"
   android:text="@{viewModel.overrideDateRange}"
   tools:text="@string/override_date_range" />

我遇到的问题是 <item name="android:drawableEnd">@drawable/ic_chevron</item> .此可绘制项目未在 api 23 及以下版本中呈现,api 24 及以上版本没问题。

编辑:

这是我的可绘制资源

<vector xmlns:android="http://schemas.android.com/apk/res/android"
  android:width="24dp"
  android:height="24dp"
  android:viewportWidth="24"
  android:viewportHeight="24">

  <path
    android:pathData="M7,19.4099l1.5443,1.5901l8.7414,-8.9993l-8.7414,-9.0007l-1.5443,1.5916l7.1956,7.4271z"
    android:strokeWidth="1"
    android:fillColor="#FFFFFF"
    android:fillType="evenOdd"
    android:strokeColor="#00000000" />

</vector>

最佳答案

这似乎是一个错误(我在运行 Android 6 的模拟器和设备中遇到过):可绘制对象已呈现,但样式属性 <item name="android:drawableTint">?colorPrimary</item>没有任何效果,所以 drawable 是白色的。

您可以通过更改 android:fillColor="#FFFFFF" 来测试它至 android:fillColor="#000000"在矢量可绘制对象的路径中。

因此您必须以编程方式为较低的 Api 级别设置颜色:

对于 Android 6,您可以引入一个颜色资源文件 res/color/my_button_tint.xml 定义 ColorStateList .由于您只需要一种颜色,因此选择器结构中的一项就足够了。但是你可以有更多的颜色depending on the Button 's state

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?android:attr/colorPrimary"/> 
</selector>

现在你可以写了

if(Build.VERSION.SDK_INT == Build.VERSION_CODES.M){
    Context ctx = myButton.getContext(); // use any available Context here
    ColorStateList csl = ContextCompat.getColorStateList(ctx, R.color.my_button_tint);
    myButton.setCompoundDrawableTintList(csl);
}

对于较低的 Api 级别,您可以更改 Drawable 的颜色通过应用 ColorFilter ,例如参见 this post

或者您只需更改 android:fillColor="#ffffff"在可绘制为所需颜色的矢量路径中。

关于Android Button drawableEnd Style Item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53506798/

相关文章:

java - setX()、setTranslationX()、setY() 和 setTranslationY()

android - 如何在不弄乱布局的情况下隐藏 TextView 和 Button?

java - 第二个 .xml 文件未在 android 上打开

java - 检查 Android 上特定设备上的蓝牙状态

android - 为什么 Maven 存储库中没有 Android SDK 4.4?

android - 底部导航 Activity 在 fragment 顶部显示意外的边距

android - 单击按钮更改 fragment View

Android键盘切割EditText

android - 使用 GStreamer 的 Android 上的 Mpeg TS 播放器

java - 在 Android Studio 中使用 Netbeans 的 JDK