簡體   English   中英

Android的多個view.xml文件以及如何使用它們

[英]Android multiple view.xml files and how to use them

我是android的新手,我正在這里尋找最佳實踐。 我有一個簡單的2視圖布局。 在頂部的文本區域中,我可以輸入產品編號,然后在其下方顯示該產品的數據。 (這是我的新手,因此產品ID現在已進行硬編碼)這​​是我的main_activity.xml

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

<TextView
    android:id="@+id/product_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_loading"/>

<View
    android:id="@+id/product_data_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

現在,查找使用互聯網,因此可以使用AsyncTask完成。

我還有另一個布局文件(product_data.xml),它列出了所有信息。 現在,我可以擁有一個大的activity_main.xml,同時具有這兩者,但是很簡單,針對不同類型的產品,將有不同的product_data.xml文件,因此我想動態地執行此操作

(我唯一的UI體驗是HTML,所以我正在看這像用jQuery結果填充DIV標簽。我想用product_data.xml布局填充@ + id / product_data_view)

我的問題是-這是正確的想法嗎? 我是在正確的道路上,還是想念基本的機器人概念?

經過一些研究,我認為我需要使用一個Intent來啟動一個加載我product_data.xml的Activity,但是我該如何將該視圖附加到主視圖的@ + id / product_data_view上? 是查看我想要的東西嗎?

您需要根據產品動態添加product_data.xml布局,然后填充該布局的數據。 檢查有關如何動態地擴展布局的問題, Android:通過擴展XML動態添加布局

希望能幫助到你。

您的目標是創建產品數據的自定義視圖。 那可以是一個自定義的列表視圖,然后可以通過Adapter類動態填充產品數據。

首先,您應該研究如何創建自定義視圖類。 這里可以作為參考[Android自定義視圖文章]

第二步,您將像這樣將自定義視圖添加到活動主體中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:custom="http://schemas.android.com/apk/res/com.your.CustomView" >


android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

<TextView
    android:id="@+id/product_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_loading"/>

<com.your.CustomView
    android:id="@+id/product_data_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

編碼愉快!

暫無
暫無

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

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