zoukankan      html  css  js  c++  java
  • 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);
    
    
        }
    }
    
  • 相关阅读:
    青春小女生 科研大梦想——微软亚洲研究院女实习生的故事
    刷新神经网络新深度:ImageNet计算机视觉挑战赛微软中国研究员夺冠
    优质博士的养成之道——对话2015微软学者奖学金获得者
    洪小文谈科研:自信与谦虚并行
    Java 8 新特性
    Java 实例
    Java 文档注释
    Java Applet 基础
    Java 多线程编程
    Java 发送邮件
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152158.html
Copyright © 2011-2022 走看看