1.用xml配置 the ImageButton defined in the following layout.
1 2 3 4 5 6 7 8
| <ImageButton Android:id="@id/rte_bold" android:layout_width="40dip" android:layout_height="40dip" android:src="@drawable/rte_bold_state" android:scaleType="fitXY" android:layout_marginTop="5dip" android:textSize="10dip" />
|
2.The rte_bold_state layout xml below, in different states(“selected” and “unselected”) show different drawbles .
1 2 3 4 5
| <?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/rte_bold_selected" /> <item android:drawable="@drawable/rte_bold" /> </selector>
|
3.下面的代码来控制是否选中,从而来改变图片:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| boldButton = (ImageButton) view.findViewById(R.id.rte_bold);
boldButton.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
if (boldButton.isSelected()) { boldButton.setSelected(false); } else { boldButton.setSelected(true); } } });
|