开发者

Android add a layout programmatically?

开发者 https://www.devze.com 2023-04-06 15:22 出处:网络
I have another class other than my main class, and inside this I want to add a layout programmatically with:

I have another class other than my main class, and inside this I want to add a layout programmatically with:

myLayo开发者_如何学Pythonut = new RelativeLayout(this);

But it crashes the app, why?


You need to add the layout params for the app. Right now your relative layout has no width or height.

ViewGroup.LayoutParams lp = new 
        LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);

myLayout = new RelativeLayout(this);
myLayout.setLayoutParams(lp);

This should work. if it doesnt put your stacktrace.

0

精彩评论

暂无评论...
验证码 换一张
取 消