android教程之用户界面篇ImageButton
图片按钮控件的使用跟其他控件的使用大同小异,就是使用整张图片作为响应按钮,而ImageButton中的图片既可以再xml文件中定义,也可以在java文件中定义,还是老规矩,我们直接上代码演示:
imagebutton.xml
<?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="vertical" > <!-- android:contentDescription 新版android系统需加上这个属性,要不然会有小警告 --> <ImageButton android:id="@+id/imgbt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/imagebutton"/> <TextView android:id="@+id/text3" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
TestImageButton.java
public class TestImageButton extends Activity{ private ImageButton imgbt; private TextView text3; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.imagebutton); imgbt=(ImageButton) this.findViewById(R.id.imgbt); imgbt.setImageResource(android.R.drawable.ic_menu_more); //使用系统自带的图片 text3=(TextView) this.findViewById(R.id.text3); imgbt.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub text3.setText("您点击的图片按钮"); } }); } }
赶快把你自己的程序运行起来吧