在的LinearLayout不调用onStart初始化 [英] linearlayout not initialised in onStart

查看:237
本文介绍了在的LinearLayout不调用onStart初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新到Android,所以不要拍我马上!

I'm fairly new to android, so don't shoot me right away!

我使用的LinearLayout在android系统来绘制图形,我画的图,设定是在LinearLayout中的背景图片。

I'm using a linearlayout in android to draw a graph, I'm drawing the graph and set is as background image in a linearlayout.

在LinearLayout中是一个相对布局的孩子,在布局XML定义:
该RelativeLayout的包含其他一些textviews和EditText上的为好。

The linearlayout is child of a relative layout and defined in the layout xml: The relativeLayout contains some other textviews and edittext's as well.

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <LinearLayout
            android:id="@+id/ll_graph"
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:orientation="vertical"
            android:layout_below="@+id/etMemo" >
        </LinearLayout>

    </RelativeLayout>

在OnCreate中:

In OnCreate:

    ll_graph = (LinearLayout) findViewById(R.id.ll_graph);

中的下一个的OnStart欲绘制图形,但是在该点的ll_graph具有0宽度和高度,因此,不显示任何内容。
如果我把窗体上的按钮来执行相同的code,宽度和高度是预期和图形绘制完美。
缺少什么我在这里?

Next in OnStart I want to draw the graph, but at that point the ll_graph has a width and height of 0, and therefore displays nothing. If I put a button on the form to execute the same code, the width and height is as expected and the graph is drawn perfectly. What am I missing here?

推荐答案

的意见是不是在这一点上还没有测量。但是,当你preSS按钮,查看

The Views are not measured at that point yet. But when you press the button, the View

为了执行时,查看刚刚从外面查看测量绘图,你可以设置preDrawListener或OnGlobalLayoutListener要查看的ViewTreeObserver。
活动上的onCreate此设置()。

In order to perform drawing when the View has just been measured from outside the View, you can set OnPreDrawListener or OnGlobalLayoutListener to View's ViewTreeObserver. Set this on onCreate() of the Activity.

    ll_graph.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            ll_graph.getViewTreeObserver().removeOnPreDrawListener(this);
            //do your stuff
            return true;
        }
    });

这篇关于在的LinearLayout不调用onStart初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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