开发者

Android add a layout programmatically?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜