梳理你的思路(从OOP到架构设计)_设计模式Android + Composite模式

目录

[1、Android + Composite模式](#1、Android + Composite模式)

2、范例之一

3、范例之二


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"); }
    };
}
相关推荐
王嘉俊92517 分钟前
设计模式--适配器模式:优雅解决接口不兼容问题
java·设计模式·适配器模式
王嘉俊92518 分钟前
设计模式--组合模式:统一处理树形结构的优雅设计
java·设计模式·组合模式
rongqing201919 分钟前
Google 智能体设计模式:多智能体协作
设计模式
李广坤15 小时前
状态模式(State Pattern)
设计模式
李广坤17 小时前
观察者模式(Observer Pattern)
设计模式
李广坤17 小时前
中介者模式(Mediator Pattern)
设计模式
李广坤18 小时前
迭代器模式(Iterator Pattern)
设计模式
李广坤18 小时前
解释器模式(Interpreter Pattern)
设计模式
阿无,21 小时前
java23种设计模式之前言
设计模式
Asort1 天前
JavaScript设计模式(八):组合模式(Composite)——构建灵活可扩展的树形对象结构
前端·javascript·设计模式