我有這個按鈕,當我點擊從
state1到state2,反之亦然時,它應該改變它的外觀。如何在android中使用onclick更改按鈕?
的心臟是2個不同的可繪製(@繪製/ ic_fav_dish_color & @繪製/ ic_fav_dish_grey)和文字是兩種不同的字符串(@字符串/ dish_faved & @字符串/ dish_not_faved)
我在XML與製作按鈕該代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/fav_dish_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_fav_dish_color"
android:gravity="start|center_vertical"
android:text="@string/dish_faved"
android:textAlignment="center"
android:layout_margin="8dp"/>
</LinearLayout>
如果我這樣做,該圖標將顯示爲按鈕的整個背景,而不是僅在左側 –
@ M.Mungenast我已經更新了我的答案。 – huk
以及如果我再次點擊,我如何實現反之亦然? –