java - 如何更改 Android 选择器中的 DrawableLeft?

标签 java android xamarin

如何使用选择器更改按钮的 DrawableLeft 图像?

我正在使用下面的选择器文件和可绘制的形状 XML。单击按钮时,图像不会改变。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
          android:drawable="@drawable/toolbar_button_pressed"
          android:drawableLeft="@drawable/ic_selected" />
    <item android:state_pressed="true"
          android:drawable="@drawable/toolbar_button_pressed" 
          android:drawableLeft="@mipmap/ic_selected" />
    <item android:drawable="@drawable/toolbar_button_normal" />
</selector>

toolbar_button_pressed

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle" >
    <corners android:radius="10dp" />
    <solid android:color="@color/primary_dark" />
    <padding android:left="10dp"
             android:top="10dp"
             android:right="10dp"
             android:bottom="10dp" />
    <stroke  android:width="1dp"
             android:color="@color/divider" />
</shape>

布局按钮

<Button android:id="@+id/background_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_background"
        android:drawableLeft="@drawable/ic_background"
        style="@style/ToolbarButton"
        android:drawablePadding="10dp"
        android:singleLine="true"
        android:layout_weight="1" />

最佳答案

我能够使用选择器解决这个问题:

<Button android:id="@+id/style_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/style_button_text"
        android:drawableLeft="@drawable/toolbar_drawable_left"
        style="@style/ToolbarButton"
        android:layout_weight="1" />

/res/drawable/toolbar_drawable_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
          android:drawable="@drawable/ic_about_highlight"/>
    <item android:state_pressed="true"
          android:drawable="@drawable/ic_about_highlight"/>
    <item android:drawable="@drawable/ic_about" />
</selector>

关于java - 如何更改 Android 选择器中的 DrawableLeft?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39479611/

相关文章:

java - Java中C++的Futures相当于什么

java - 获取排序的 TableModel

Android操作栏标题颜色不改变

ios - 未找到授权文件 "Entitlements.plist"。错误

ios - Xamarin.iOS 设计器 - 创建 Action 转场不起作用

java - 如何使用 java 在 Mongo 3.x 中执行文本搜索

java - 如何在elasticsearch中从父id获取子列表

java - ProgressDialog 未显示在具有自定义 View 的 Activity 中

java - 图像按钮消失

ios - GetDescriptor() 空值/128 位 UUID 描述符?