1. mail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_marginTop="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/button1"
android:layout_width="250dip"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/buttonstyle"
android:textSize="20sp"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip">
</Button>
</RelativeLayout>
</LinearLayout>
2. res/drawable/buttonstyle.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/button_selected" />
<item android:state_focused="true" android:state_enabled="true"
android:state_window_focused="true" android:drawable="@drawable/button_focused" />
<item android:drawable="@drawable/button_defalut" />
</selector>
3. demobutton.java
package com.demobutton;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.Html.ImageGetter;
import android.widget.Button;
public class demobutton extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageGetter imgGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = null;
drawable = demobutton.this.getResources().getDrawable(
Integer.parseInt(source));
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
return drawable;
}
};
Button button1 = (Button) findViewById(R.id.button1);
StringBuffer dbutton = new StringBuffer();
dbutton.append("<img src=\"").append(R.drawable.button1).append("\"/>").append("<font color=\"6f6f6f\">按鈕上加圖片效果</font>");;
Spanned span = Html.fromHtml(dbutton.toString(), imgGetter, null);
button1.setText(span);
dbutton = null;
}
}