查看通货膨胀和自定义视图 [英] View Inflation and custom views

查看:147
本文介绍了查看通货膨胀和自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序的自定义视图我画使用的OnDraw()功能视图。此外,它需要从活动的一些数据来绘制图形。因此,而不是使用标准的的setContentView(R.layout.myview)我现在用的是以下 -

I have a custom view in my app that I draw using the onDraw() function in the View. Also it needs some data from the ACtivity to draw the graphic. So instead of using the standard setContentView(R.layout.myview) I am using the following -

MyView mv = new MyView(this, userData);
setContentView(mv);

这似乎工作,直到我添加了一个的TextView 的customview以上。后来我才意识到,上述code不显示的TextView的。另外, onFinishInflate()永远不会被调用。我一定要夸大布局自己在这种情况下?如果是的话我必须调用的OnDraw()发挥自己呢?

This seemed to work until I added a textview above the customview. Then I realized that the above code does not show the textview at all. Also the onFinishInflate() is never called. Do I have to inflate the layout myself in this case? If so do I have to call the onDraw() function myself too?

谢谢, - P

推荐答案

你应该在这里是一个包含布局的TextView 和您的 MyView的,然后你的活动里,找到你的自定义视图,并通过在用户数据。你的 MyView的在其可以使用这个的OnDraw()。也许是这样的:

What you should have here is a layout that contains the TextView and your MyView and then inside your activity, find your custom view and pass in your user data. Your MyView can then use this during its onDraw(). Perhaps something like this:

RES /布局/ main.xml中

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="veritcal">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World" />
    <my.package.MyView
        android:id="@+id/myview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

            

的src /我/封装/ MyView.java

src/my/package/MyView.java:

public class MyView extends View {
    UserData mUserData = null;
    public void setUserData(userData) {
        mUserData = userData;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        performCustomDrawingWithUserData(mUserData);
        super.onDraw(canvas);
    }

}

的src /我/封装/ MyActivity.java

public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // pass the user data into myview here.
        MyView myView = (MyView) findViewById(R.id.myview);
        myView.setUserData(userData);
    }

}

这篇关于查看通货膨胀和自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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