1、定义一个button.xml的layout文件,实现button内容(图+文字)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
android:src="@drawable/p11"
android:scaleType="fitXY"
android:paddingTop="0dip"
android:paddingBottom="5dip"
android:paddingLeft="0dip"
android:layout_gravity="center_vertical"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:textColor="#000000"
android:id="@+id/tv"
android:layout_marginLeft="8dip"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
2、定义一个ImageBt类,继承LinearLayout
public class ImageBt extends LinearLayout{
private ImageView iv;
private TextView tv;
public ImageBt(Context context)
{
this(context, null);
}
public ImageBt(Context context, AttributeSet attrs)
{
super(context, attrs);
// 导入布局
LayoutInflater.from(context).inflate(R.layout.button, this, true);
iv = (ImageView) findViewById(R.id.iv);
tv = (TextView) findViewById(R.id.tv);
}
// 设置图片资源
public void setImageResource(int resId)
{
iv.setImageResource(resId);
}
//设置显示的文字
public void setTextViewText(String text)
{
tv.setText(text);
}
}
3、修改activity_main.xml,定义需要的按钮
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button state"
android:textColor="#000000"
android:id="@+id/tv1"
android:layout_marginLeft="8dip"
android:layout_gravity="center_vertical"
/>
<com.example.test.ImageBt
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv1"
android:layout_below="@+id/tv1"
android:layout_marginTop="43dp"
android:background="@drawable/p11"
android:clickable="true"
android:focusable="true" >
</com.example.test.ImageBt>
<com.example.test.ImageBt
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/confirm"
android:layout_centerHorizontal="true"
android:background="@drawable/p11"
android:clickable="true"
android:focusable="true" >
</com.example.test.ImageBt>
</RelativeLayout>
4、编写MainActivity.java文件,使用声明的按钮
public class MainActivity extends Activity {
private ImageBt ib1;
private ImageBt ib2;
TextView tv1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.tv1);
ib1 = (ImageBt) findViewById(R.id.confirm);
ib2 = (ImageBt) findViewById(R.id.cancel);
ib1.setTextViewText("确定");
ib1.setImageResource(R.drawable.p11);
ib2.setTextViewText("取消");
ib2.setImageResource(R.drawable.p11);
ib1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//在这里可以实现点击事件
tv1.setText("button OK");
}
});
ib2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//在这里可以实现点击事件
tv1.setText("button CENCEL");
}
});
}
5、修改AndroidManifest.xml文件,添加android:<!-- button.xml -->语句,声明button.xml