本文共 2381 字,大约阅读时间需要 7 分钟。
修改UI-XML文件如下:(记得在res/drawable/下放图片)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageButton android:id="@+id/okButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ok"/> <ImageButton android:id="@+id/exitButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/exit"/> </LinearLayout> <Button android:id="@+id/cancelButton" android:layout_marginTop="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff0000" android:text="取消"/> </LinearLayout>
activity主类的代码如下:(事件处理逻辑)
package com.scholar.luo;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageButton;/** * * @author scholar.luo * 基于xml-base的LinearLayout的demo * */public class LinearLayoutDemo extends Activity implements OnClickListener{ private ImageButton iExitButton = null; private ImageButton iOkButton = null; private Button iCancelButton = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //从资源文件main.xml中获取按钮对象的引用 //注意:必顺解释main.xml文件,生产相应的对象后也得获取其引用 //因此先运行setContentView(R.layout.main) iExitButton = (ImageButton)findViewById(R.id.exitButton); iOkButton = (ImageButton)findViewById(R.id.okButton); iCancelButton = (Button)findViewById(R.id.cancelButton); //注册事件临听者 iExitButton.setOnClickListener(this); iOkButton.setOnClickListener(this); iCancelButton.setOnClickListener(this); } /** * 点击事件处理 * @param aView */ public void onClick(View aView) { if (aView.equals(iOkButton)) { this.setTitle("你按下了确定按钮"); } else if (aView.equals(iCancelButton)) { this.setTitle("你按下了取消按钮"); } else if (aView.equals(iExitButton)) { this.finish(); } } }
转载地址:http://cbmbi.baihongyu.com/