• 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);
    
    
        }
    }
    
  • 相关阅读:
    【转载】[教程]OpenSEES超简单易懂的入门第一课
    【转载】面向对象的非线性有限元方法
    与李文雄老师讨论有限元
    【转载】 Moving Beyond OpenGL 1.1 for Windows
    【转载】国外免费期刊全文数据库
    与李文雄老师讨论学术研究
    【转载】VS 2010和.NET 4.0之WPF 4改进全解析
    【转载】MFC中SDI、MDI框架各部分指针获取(网上找的,好东西大家一起分享,多谢原创作者!)
    【转载】一位院士——搞科研的几个条件
    Visual Studio 2010 step by step学习摘要
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152158.html
Copyright © 2020-2023  润新知