26 用代码创建布局并添加

案例1

package com.qf.sxy.day30_javalayout;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        //根布局
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setBackgroundColor(Color.BLUE);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0);
        params.weight = 1;

        Button button1 = new Button(this);
        button1.setBackgroundColor(Color.RED);
        button1.setLayoutParams(params);

        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0);
        params2.weight = 2;

        Button button2 = new Button(this);
        button2.setBackgroundColor(Color.BLACK);
        button2.setLayoutParams(params2);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"国庆快乐",Toast.LENGTH_SHORT).show();
            }
        });

        linearLayout.addView(button1);
        linearLayout.addView(button2);

        setContentView(linearLayout);



    }
}

案例二

package com.qf.sxy.day30_javalayout;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

public class RelativeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_relative);

        RelativeLayout relativeLayout = new RelativeLayout(this);

        Button centerButton = new Button(this);
        //通过资源设置id
        centerButton.setId(R.id.btn_relative);
        centerButton.setText("放假了");
        RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        centerParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        centerButton.setLayoutParams(centerParams);


        Button centerButton1 = new Button(this);
        centerButton1.setText("7天啊");
        RelativeLayout.LayoutParams centerParams1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        centerParams1.addRule(RelativeLayout.RIGHT_OF,centerButton.getId());
        centerParams1.addRule(RelativeLayout.BELOW,centerButton.getId());
        centerButton1.setLayoutParams(centerParams1);


        Button centerButton2 = new Button(this);
        centerButton2.setText("有作业啊");
        RelativeLayout.LayoutParams centerParams2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        centerParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        centerButton2.setLayoutParams(centerParams2);

        relativeLayout.addView(centerButton);
        relativeLayout.addView(centerButton1);
        relativeLayout.addView(centerButton2);

        setContentView(relativeLayout);


    }
}
原文地址:https://www.cnblogs.com/muyuge/p/6152158.html