入门Android实例开发--动态加载组件

package com.tpout.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by TpOuT on 2/17/2016.
 */
public class DynamicLoading extends AppCompatActivity{


        private LinearLayout myLayout;
        private LinearLayout.LayoutParams layoutP;
        private int WC= LinearLayout.LayoutParams.WRAP_CONTENT;
        private TextView tv;
        private Button b;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            myLayout=new LinearLayout(this);		//实例化布局对象
            myLayout.setOrientation(LinearLayout.VERTICAL);	//设置

            setContentView(myLayout);	//加载布局
            layoutP=new LinearLayout.LayoutParams(WC,WC);

            tv=new TextView(this);
            b=new Button(this);

            myLayout.addView(tv,layoutP);//添加组件
            myLayout.addView(b,layoutP);

            tv.setText("这是tv文本");
            b.setText("这是b文本");
        }
}
效果图:

入门Android实例开发--动态加载组件_第1张图片

你可能感兴趣的:(入门Android实例开发--动态加载组件)