目录
[1、Android + Composite模式](#1、Android + Composite模式)
1、Android + Composite模式
- 在Android平台里,像Button或ImageButton等屏幕控件皆通称为View。
- 多个View能组合在一起,就会各种排列方式,即称为「布局」 (Layout)。
- 这Layout类别就是从ViewGroup父类别衍生出来的。
- 最基本的布局方式有二,就是:垂直和水平排列。
- 例如,画面里有三个按钮:
- 其中,把OK和Exit两个按钮看成一组,构成一组垂直型的布局,称为垂直LinearLayout。
- 然后,把这个垂直LinearLayout与Cancel按钮看成一组,构成一组水平型的布局,称为水平LinearLayout。
2、范例之一
java
// Axc01.java
// ......
public class Ax01 extends Activity implements OnClickListener {
private final int WC =
LinearLayout.LayoutParams.WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
private Button btn, btn2;
private EditText et;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
et = new EditText(this);
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(FP, WC);
layout.addView(et, param);btn = new Button(this); btn.setText("OK");
LinearLayout.LayoutParams param2 =
new LinearLayout.LayoutParams(WC, WC);
param2.topMargin = 5;
btn.setBackgroundResource(R.drawable.ok_blue);
btn.setOnClickListener(this);
layout.addView(btn, param2);
btn2 = new Button(this);
btn2.setTextColor(Color.RED); btn2.setText("Exit");
btn2.setBackgroundResource(R.drawable.exit_gray);
btn2.setOnClickListener(this);
layout.addView(btn2, param2);
setContentView(layout);
}
public void onClick(View v) {
if(v == btn)
setTitle(et.getText());
else if(v == btn2)
finish();
}
}
3、范例之二
java
// Ax02.java
// ........
public class Ax02 extends Activity {
private final int WC =
LinearLayout.LayoutParams.WRAP_CONTENT;
@Override public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(70, 45);
param.topMargin = 5;
Button btn = new Button(this);
btn.setBackgroundResource(R.drawable.ok);
btn.setText("OK"); btn.setOnClickListener(listener);
layout.addView(btn, param);Button btn2 = new Button(this);
btn2.setBackgroundResource(R.drawable.heart);
btn2.setText("Exit"); btn2.setOnClickListener(listener2);
layout.addView(btn2, param);
LinearLayout out_layout = new LinearLayout(this);
out_layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams out_param =
new LinearLayout.LayoutParams(WC, WC);
out_layout.addView(layout, out_param);
Button btn3 = new Button(this);
btn3.setText("Cancel"); btn3.setTextColor(Color.WHITE);
btn3.setBackgroundColor(Color.RED);
btn3.setOnClickListener(listener3);
param.leftMargin = 5;
out_layout.addView(btn3, param);
setContentView(out_layout);
}
OnClickListener listener = new OnClickListener(){
public void onClick(View v) {
setTitle("this is OK button");
}
};
OnClickListener listener2 = new OnClickListener() {
public void onClick(View v) { finish(); }
};
OnClickListener listener3 = new OnClickListener(){
public void onClick(View v){ setTitle("this is Cancel button"); }
};
}