程序所用的图片 看代码 package com.scholar.luo; import android.app.Activity; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.LinearLayout; /** * 基于code-based 的LinearLayout的Demo * UI-TREE * ViewGroup2 * | * ViewGroup1 iCancelButton * | * iOkButton iExitButton * @author scholar.luo * */ public class LinearLayutDemo extends Activity implements OnClickListener{ private ImageButton iExitButton = null; private ImageButton iOkButton = null; private Button iCancelButton = null; private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //ViewGroup1-包含iExitButton与iOkButton,垂直排版 LinearLayout sVerticalLayout = new LinearLayout(this); sVerticalLayout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams sLayoutParams = new LinearLayout.LayoutParams(WC, WC); iOkButton = new ImageButton(this); Drawable sBackgroundImage = this.getResources().getDrawable(R.drawable.ok); iOkButton.setImageDrawable(sBackgroundImage); iOkButton.setOnClickListener(this); iExitButton = new ImageButton(this); sBackgroundImage = this.getResources().getDrawable(R.drawable.exit); iExitButton.setImageDrawable(sBackgroundImage); iExitButton.setOnClickListener(this); sVerticalLayout.addView(iOkButton, sLayoutParams); sVerticalLayout.addView(iExitButton, sLayoutParams); //ViewGroup2-包含ViewGroup1与iCancelButton,水平排版 LinearLayout sHorizontalLayout = new LinearLayout(this); sHorizontalLayout.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.LayoutParams sLayoutParams2 = new LinearLayout.LayoutParams(WC, WC); sLayoutParams2.topMargin = 30; iCancelButton = new Button(this); iCancelButton.setText("取消"); iCancelButton.setBackgroundColor(Color.RED); iCancelButton.setOnClickListener(this); sHorizontalLayout.addView(sVerticalLayout, sLayoutParams); sHorizontalLayout.addView(iCancelButton, sLayoutParams2); setContentView(sHorizontalLayout); } /** * 点击事件处理 */ 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(); } } }