Android设备上查看实例化顺序 [英] Android View Instantiation Order

查看:139
本文介绍了Android设备上查看实例化顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我缺少一个关键的概念,消息队列,同时创造我的自定义视图,并试图不使用它竟然是我有问题的源更新视图。这被这里清理:

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:

<一个href=\"http://stackoverflow.com/questions/12955899/android-writing-custom-views-that-support-the-message-queue\">Android:编写支持消息队列定制视图

原来,你需要让OnCreate中退出(像正常)后,运行时间/ Android的电话OnMeasure,并在平均时间把任何更新,通过方法.post的在消息队列中的视图。

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.

所以,你可以抓住的句柄视图,通过FindViewById,在OnCreate中像往常一样,然后
发布的任何更新(无论是通过类似的setText,或者你自己的一套功能覆盖)
在可运行(见上面的链接)。

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).

我一直在努力自定义视图在android系统相当长的一段时间,并已与throughly构造函数,属性,布局参数试验,并绘制方法(OnMeasure,OnLayout,...)。

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, ...).

家长最ViewGroup中(一个在渲染布局文件中声明)从RelativeLayout的继承,并包含三个子视图,所有的自定义/继承viewgroups为好,等等。

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.

我实现自定义日历(我知道,非常原始和令人兴奋的),我会打电话给CustomCalendar。所以,在我的布局文件我有说明书

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.

我试图找出如何得到这个观点的尺寸OnMeasure被调用之前(在那里我可以让他们没有问题,但是由于孩子意见的布局尺寸从这个观点尺寸决定,我不能实例化childeren意见直至第一次OnMeasure被调用,这被证明是后我需要它;之后的OnCreate退出)

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).

也就是说,调用的setContentView在OnCreate中,然后从实例通过它FindViewById(Resource.Id.Calendar1)自定义视图后,孩子的观点没有得到实例化,以及如果从OnMeasure倍率(再做,B / C OnMeasure不叫,直到之后的OnCreate回报)。

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).

如果我读传递到构造属性,承滴盘我从layout_width获取和layout_height是常数-1 FILL_PARENT。我不能只是使用整个屏幕要么,因为有以弥补其他意见。

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.

因此​​,我决定从它的构造函数中的自定义视图的维度寻找最好的方法,这样我可以从它(构造函数)用适当的布局尺寸实例化所有视图的子女的,这样的看法可从OnCreate中(与内容更新)的setContentView调用后,并从OnCreate中返回之前。

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).

推荐答案

您无法获得在构造尺寸。在最好的情况,如果他们被定义在XML中,你可以得到从AttributeSet中的布局宽度和高度。

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.

你应该做的是覆盖onMeasure和onLayout。你可以做你onMeasure需要的任何计算和设置,然后通过计算值 measureChild(查看,widthSpec,heightSpec)。你可以在onLayout布局乱七八糟的东西,然后调用 childView.layout(左,上,右,下)

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).

这篇关于Android设备上查看实例化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆