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);
    
    
        }
    }
    
  • 相关阅读:
    屏幕的真实分辨率大小
    CCConfiguration::sharedConfiguration()->loadConfigFile cocos2d-x 中文乱码问题及国际化解决方案
    git 放弃提交到提交之前
    cocos2d-x 输出debug信息
    Ubuntu设置环境变量
    有时候需要统计手机的型号和版本号,利用程序可以获取到相应的手机信息.
    读取 android sys/下的信息
    android 读取 raw 中的文件。
    C/C++中结构体(struct)
    异步图片下载引擎(升级版——ExecutorService+handler)
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152158.html
Copyright © 2011-2022 走看看