梳理你的思路(从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"); }
    };
}
相关推荐
电子科技圈6 分钟前
先进封装、芯粒架构和3D集成——先进异构集成亟需兼具标准化与定制化能力的互联及总线IP解决方案
tcp/ip·设计模式·架构·软件构建·代码规范·设计规范
zjun10012 小时前
C++:2.工厂模式
设计模式
触底反弹3 小时前
🤯 面试被问 AI Workflow 和 Agent 有啥区别?3 张图 + 2 段代码讲清楚!
人工智能·设计模式·面试
杨充1 天前
10.可测试性实战设计
设计模式·开源·代码规范
杨充1 天前
9.重构十二式的实战
设计模式·开源·代码规范
杨充1 天前
6.设计原则的全景图
设计模式·开源·全栈
杨充1 天前
2.面向对象的特性
设计模式
杨充1 天前
7.SOLID原则案例汇
设计模式·开源·全栈
杨充1 天前
8.反模式与坏味道
设计模式·开源·代码规范
杨充1 天前
3.接口vs抽象类比较
设计模式