簡體   English   中英

如何在android中使用相同的XML布局創建多個活動,每個活動都有自己的自定義視圖

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

Android編碼的新手,但是我找不到這個特定問題的答案。 說我有以下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>

我想使用這種布局來創建多個活動,但是在每個活動中我都想使用不同的customView。 例如。 對於一個活動,我希望自定義視圖為customView1,對於第二個活動,我希望其為customView2,依此類推。

我將如何去做? 現在,我的每個活動都只有一個單獨的XML布局,這似乎非常多余。

您要么創建多個布局,要么必須以編程方式添加新視圖(通過創建適當類型的對象並將其添加到父視圖),而不是通過xml來完成。 xml布局背后的想法是您的布局或多或少是靜態的,因此xml精確地指定了您想要的內容。 它本身並不是要改變一切的編程語言。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM