梳理你的思路(从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"); }
    };
}
相关推荐
ss2735 小时前
手写MyBatis第32弹-设计模式实战:Builder模式在MyBatis框架中的精妙应用
设计模式·mybatis·建造者模式
汤姆大聪明5 小时前
【软件设计模式】策略模式
设计模式·策略模式
pengzhuofan8 小时前
Java设计模式-模板方法模式
java·设计模式·模板方法模式
使二颗心免于哀伤8 小时前
《设计模式之禅》笔记摘录 - 17.模板方法模式
笔记·设计模式·模板方法模式
AlenLi1 天前
JavaScript - 观察者模式的实现与应用场景
前端·设计模式
pengzhuofan1 天前
Java设计模式-享元模式
java·设计模式·享元模式
希望_睿智1 天前
实战设计模式之解释器模式
c++·设计模式·架构
楚禾Noah1 天前
【设计模式实战】原型模式 + 工厂模式:AI Agent 配置中心
人工智能·设计模式·原型模式
Pure_Eyes1 天前
设计模式详解
设计模式
hai_qin1 天前
三,设计模式-抽象工厂模式
c++·设计模式·抽象工厂模式