简体   繁体   English

如何在android中使用相同的XML布局创建多个活动,每个活动都有自己的自定义视图

[英]How to use same XML layout in android to create several activities, each with their own custom view

New to coding to Android, but I couldn't find an answer to this particular problem. Android编码的新手,但是我找不到这个特定问题的答案。 Say I have the following XML layout 说我有以下XML布局

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

    <view class="com.example.testapp.customView"
        android:id="@+id/drawView"
        android:layout_height="100dp"
        android:layout_width="fill_parent" />

    <Button android:id="@+id/bNormal"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Update" />

</LinearLayout>

I want to use this layout to create several activities, but in each activity I want to use a different customView. 我想使用这种布局来创建多个活动,但是在每个活动中我都想使用不同的customView。 Eg. 例如。 for one activity I would like the custom view to be customView1, the second activity I would like it to be customView2, etc. 对于一个活动,我希望自定义视图为customView1,对于第二个活动,我希望其为customView2,依此类推。

How would I go about doing that? 我将如何去做? Right now I just have a seperate XML layout for each of my activities, and this seems awfully redundant. 现在,我的每个活动都只有一个单独的XML布局,这似乎非常多余。

You either create several layouts, or you have to add the new view programatically (by creating an object of the proper type and adding it to the parent view) rather than doing it via xml. 您要么创建多个布局,要么必须以编程方式添加新视图(通过创建适当类型的对象并将其添加到父视图),而不是通过xml来完成。 The idea behind xml layouts is that your layout is more or less static, so the xml specifies exactly what you want. xml布局背后的想法是您的布局或多或少是静态的,因此xml精确地指定了您想要的内容。 It isn't meant to be a programming language itself where things can change. 它本身并不是要改变一切的编程语言。

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

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