创建 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);
}
};
编辑:
第二个选项,如果你想使用按钮视图创建一个类似按钮的图像,然后创建一个自定义按钮:
首先将所有图像(例如按下、聚焦和默认)放在 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
为:
<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 中创建自定义的精美按钮