简体   繁体   中英

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. Say I have the following XML layout

<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. Eg. for one activity I would like the custom view to be customView1, the second activity I would like it to be customView2, etc.

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.

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. The idea behind xml layouts is that your layout is more or less static, so the xml specifies exactly what you want. It isn't meant to be a programming language itself where things can change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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