【问题标题】:Change drawable color on button on click单击时更改按钮上的可绘制颜色
【发布时间】:2013-07-26 10:43:09
【问题描述】:

我在 xml 中有一些带有文本和可绘制集的按钮,带有 android:drawableLeft=...

我想知道如何在点击时更改文本和可绘制对象的颜色。而且它必须是通用的,因为我每次都有很多按钮,不同的drawable。

我的 XML :

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_articles"
        style="@style/Main_button"
        android:layout_marginLeft="5dp"
        android:background="@color/green3"
        android:onClick="myClickHandler"
        android:text="Articles"
        android:drawableLeft="@drawable/ic_article"
        android:textColor="@drawable/btn_default" />

    <Button
        android:id="@+id/btn_encaiss"
        style="@style/Main_button"
        android:layout_marginLeft="5dp"
        android:background="@color/green4"
        android:onClick="myClickHandler"
        android:text="Encaissement"
        android:drawableLeft="@drawable/ic_encaiss"
        android:textColor="@drawable/btn_default" />

    <Button
        android:id="@+id/btn_stats"
        style="@style/Main_button"
        android:layout_marginLeft="5dp"
        android:background="@color/green1"
        android:onClick="myClickHandler"
        android:text="Statistiques"
        android:drawableLeft="@drawable/ic_stats"
        android:textColor="@drawable/btn_default" />

    <Button
        android:id="@+id/btn_clients"
        style="@style/Main_button"
        android:layout_marginLeft="5dp"
        android:background="@color/green2"
        android:onClick="myClickHandler"
        android:text="Clients"
        android:drawableLeft="@drawable/ic_clients"
        android:textColor="@drawable/btn_default" />
</LinearLayout>

现在我只是在点击时更改文本颜色。我想在不知道左侧drawble名称的情况下同时更改drawable和文本颜色。

【问题讨论】:

  • 请粘贴xml。

标签: android button colors onclick drawable


【解决方案1】:
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
img.setBounds( 0, 0, 60, 60 );
txtVw.setCompoundDrawables( img, null, null, null );

使用此代码,您可以通过编程方式更改左侧可绘制对象。

文字颜色见Android documentation

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 1970-01-01
    • 2021-07-07
    • 2012-12-06
    • 2016-06-08
    • 2016-09-08
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    相关资源
    最近更新 更多