创建 ImageButton 为:
在main.xml中:
<ImageButton android:id="@+id/ib"
android:src="@drawable/bookmark" <-- SET BUTTON IMAGE HERE -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
在代码部分:
ImageButton ib=(ImageButton)findViewById(R.id.ib);
ib.setOnClickListener(ibLis);
}
private OnClickListener ibLis=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//START YOUR ACTIVITY HERE AS
Intent intent = new Intent(YOUR_CURRENT_ACTIVITY.this,NextActivity.class);
startActivity(intent, 0);
}
};
EDIT:
如果您想使用按钮视图创建类似按钮的图像,则选择第二个选项,然后创建自定义按钮:
首先将所有图像(例如按下的、聚焦的和默认的图像)放入 res/drawable 文件夹中,然后在 drawable/newbtn.xml 中添加 newbtn.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_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
最后在按钮 XML 集中android:background
as :
<Button
android:id ="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textColor="#ffffffff"
android:background="@drawable/newbtn" <-- get button background to selector -->
/>
请参阅本教程,了解使用图像创建自定义按钮
在 Android 中创建自定义、精美的按钮 http://www.thesecretpie.com/2010/07/creating-custom-fancy-buttons-in.html