猿问

关于动态加载布局错误

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
;

        
}

    }

}

doopsoft
浏览 1123回答 2
2回答

行深般若波罗蜜多时

你这代码以后粘贴上来注意下格式,不然别人给你看一点不方便,特别是长的代码

蜂之谷

醉了,这怎么看,代码贴好一点
随时随地看视频慕课网APP

相关分类

Android
我要回答