package com.doop.multilayoutdemo ; import android.os.Bundle ; import android.support.v7.app.AppCompatActivity ; import android.view.LayoutInflater ; import android.view.View ; import android.widget.Button ; import android.widget.LinearLayout ; import android.widget.RelativeLayout ; //在后面添加implements View.OnClickListener public class MainActivity extends AppCompatActivity implements View.OnClickListener { private RelativeLayout FatherLL ; private LinearLayout ChildLL1 ; private LinearLayout ChildLL2 ; private Button btnloadLayout ; private RelativeLayout.LayoutParams RLP ; @Override protected void onCreate ( Bundle savedInstanceState) { super . onCreate (savedInstanceState) ; setContentView ( R.layout . activity_main ) ; init () ; //初始化 } //初始化 private void init () { btnloadLayout = ( Button ) findViewById ( R.id . btn_openChildLayout ) ; //定义按钮引用 FatherLL = ( RelativeLayout ) findViewById ( R.id . activity_main ) ; //定义布局变量引用 View view1 = LayoutInflater . from ( this ). inflate ( R.layout . acttivity_child , null, false ) ; ChildLL1 = ( LinearLayout ) view1 . findViewById ( R.id . child1 ) ; View view2 = LayoutInflater . from ( this ). inflate ( R.layout . activity_child2 , null, false ) ; ChildLL2 = ( LinearLayout ) view2 . findViewById ( R.id . child2 ) ; btnloadLayout . setOnClickListener ( this ) ; //btn_openChildLayout添加单击监听 } public void onClick ( View view) { switch (view. getId ()) { case R.id . btn_openChildLayout : RelativeLayout.LayoutParams RLP = new RelativeLayout.LayoutParams ( RelativeLayout.LayoutParams . WRAP_CONTENT , RelativeLayout.LayoutParams . WRAP_CONTENT ) ; RLP . setMargins ( 16 , 16 , 16 , 16 ) ; FatherLL . addView ( ChildLL1 , RLP ) ; ChildLL1 . setVisibility ( View . VISIBLE ) ; break ; default : break ; } } }
行深般若波罗蜜多时
蜂之谷
相关分类