开发者

Width is 0 after adding a child

开发者 https://www.devze.com 2023-02-20 06:06 出处:网络
I am making an application that need to have layout items loaded dynamic. I have an XML where I have the design of the element that will be added multiple times on the stage. Also, I have a RelativeLa

I am making an application that need to have layout items loaded dynamic. I have an XML where I have the design of the element that will be added multiple times on the stage. Also, I have a RelativeLayout that will hold the elements. To add an element to the RelativeLayout I use this:

...   
View child = View.inflate(context, R.layout.buildinglayout, null);
parrent.addView(child, parrent.getChildCount()); //parrent is the relativeLayout
...
开发者_StackOverflow

As you may guessed, buildinglayout.xml is the layout of the element. Everything is added to the stage correctly and apparently with no problems. The thing is that the element's to fit on the stage so I need to have a ScrollView that surrounds the RelativeLayout. The only problem is that the scrollview does not detect the actual width and height of the RelativeLayout. So...I did this:

...
View child = View.inflate(context, R.layout.buildinglayout, null);
parrent.addView(child, parrent.getChildCount()); //parrent is the relativeLayout
Log.v("BuildingView", "w:" + child.getWidth() + " h:" + child.getHeight());
Log.v("ParrentView", "w:" + parrent.getWidth() + " h:" + parrent.getHeight());
...

The output of those Log.v is:

03-29 06:56:43.803: VERBOSE/BuildingView(480): w:0 h:0
03-29 06:56:43.803: VERBOSE/ParrentView(480): w:0 h:752

Can anyone please tell me what to do so that the width and height of both the buildingview and the parrent to be corect?

P.S. I am using Android 3.0 on an emulator device


The inflate is ignoring the layout height and width specified in the xml. Either set the layout params of child view or check this answer on how to inflate the view.


The view that is added in UI thread will reflect after UI thread completes.

Try to find width after UI thread completes.

Then only it will give actual width and height.

0

精彩评论

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

关注公众号