Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

c# - Android View Instantiation Order

EDIT: I was missing a key concept, the message queue, while creating my custom view and trying to update the view without using it turned out being the source of the issue I was having. This gets cleared up here:

Android: Writing Custom Views that Support The Message Queue

It turns out that you need to let the runtime/android call OnMeasure after OnCreate exits ("like normal"), and in the mean time put any updates to the view in the message queue via the .post method.

So, you can grab a handle to the view, via FindViewById, in OnCreate as usual, and then post any updates to it (either through overrides like setText, or your own set functions) within a runnable (see above link).


I've been working on a custom view in android for quite some time now and have throughly experimented with constructors, attributes, layout parameters, and drawing methods (OnMeasure, OnLayout, ...).

The parent-most viewgroup (the one declared in the layout file for rendering) inherits from RelativeLayout and contains three child views, all custom/inherited viewgroups as well, and so on.

I'm implementing a custom calendar (I know, very original and exciting), I'll call CustomCalendar. So, in my layout file I have the statment

<MonoDroid.CustomViews.CustomCalendar
  xmlns:calendar="http://schemas.android.com/apk/res/net.monocross.appname"
  android:id="+id/Calendar1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  calendar:row_count="5"
  calendar:col_count="7"
 ...
/>

This view resides along side a few others that are defined in the layout file as well, and thus the source of my complication, which will be stated now.

I'm trying to figure out how to get this view's dimensions before OnMeasure is called (where I can get them no problem, however because the layout dimensions of the children views are determined from this views dimensions, I cannot instantiate the childeren views until the first time OnMeasure is called, and this proves to be after I need it; after OnCreate exits).

That is, after calling SetContentView in OnCreate, and then instantiating the custom view from it via FindViewById(Resource.Id.Calendar1), the child views do not get instantiated as well if its done from the OnMeasure override (again, b/c OnMeasure isn't called until after OnCreate returns).

If I read the attributes passed into the constructor, alls I get from layout_width and layout_height are the constant -1 for fill_parent. I cannot just use the entire screen either, because there are other views to compensate for.


Therefore, I'm looking for the best approach in determining the dimensions of a custom view from within its constructor so that I can instantiate all of the view's childern from it (the constructor) with the proper layout dimensions so that the views are available from OnCreate (for updating with content) after SetContentView is called and before returning from OnCreate.

Another means to the same end will be fine with me, though I'm kinda looking to do this the way the infrastructure intends it to be (if there is such a thing).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't get dimensions in the constructor. At best, if they are defined in XML, you could get the layout width and height from the AttributeSet.

What you should be doing is overriding onMeasure and onLayout. You can do whatever calculations and setup you need in onMeasure, then pass the calculated values to measureChild(view, widthSpec, heightSpec). and you can mess with layout stuff in onLayout then call childView.layout(left, top, right, bottom).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...