简体   繁体   English

如何使这个Android项目的自定义XML布局?

[英]How to make the custom xml layout for this android project?

I am trying make application from this project: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html 我正在尝试从此项目制作应用程序: http : //developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html

But here, there is a Layout that generating dynamicly. 但是这里有一个动态生成的布局。 I want to create my own layout in xml file. 我想在xml文件中创建自己的布局。 So what should i have to do for it. 所以我该怎么办。

Please anyone can help me to make the xml layout from this dynamic layout ?? 请任何人可以帮助我从此动态布局制作xml布局? Thanks. 谢谢。

That example is not creating a "dynamic layout". 该示例未创建“动态布局”。 The layout, which is the part you'd be defining in XML, consists of only one View object, MyView . 布局是您要在XML中定义的部分,仅包含一个View对象MyView

What I assume you are referring to by "dynamic layout" is the MyView class, which is a custom View object which accepts touch input and draws on the screen. 我认为您MyView的“动态布局”是MyView类,它是一个自定义View对象,它接受触摸输入并在屏幕上绘制。 This cannot be defined in XML... you must write the Java code to handle the logic necessary, since the regular View class (which MyView is extending) does not support such functionality. 这不能用XML定义...您必须编写Java代码来处理必要的逻辑,因为常规的View类(MyView扩展了该类)不支持此类功能。

What you would need to do is create a Java file defining the MyView class. 您需要做的是创建一个定义MyView类的Java文件。 Say for example, com.example.MyView . 例如, com.example.MyView Then, in XML, you can include this custom view in your layout by referring to the full name, including the package name. 然后,在XML中,您可以通过引用全名(包括包名)在布局中包括此自定义视图。 For example... 例如...

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:orientation="vertical">
        <com.example.MyView>
        android:layout_height="fill_parent"
            android:layout_width="fill_parent"
        </com.example.MyView>
    </LinearLayout>

You can use this layout in an activity as usual using setContentView . 您可以像往常一样使用setContentView在活动中使用此布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM