簡體   English   中英

Android:如何以相似的布局簡化各種活動的實現

[英]Android: How to Simplify Implementation of Various Activities with Similar Layout

最近,我正在編寫一個Android Apps來控制某些外部設備。 有不同種類的設備,但它們非常相似。 因此,用於控制設備的每個活動的布局也非常相似。 例如,所有布局基本上都具有相同的文本視圖,編輯視圖和按鈕。 區別在於一台設備具有列表視圖,而其他設備沒有列表視圖。

現在,我只復制每個設備的每個布局(以及相應的活動),並在每個活動中更改一些內容。 我覺得這是多余的,我認為必須有另一種更簡單/更有效的方法來實現它,但是怎么辦呢?

謝謝。

您可以使用標簽include重用布局的公共部分

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_bg"
android:gravity="center_horizontal">

<include layout="@layout/example"/>

<TextView android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello"
          android:padding="10dp" />

...

在這種情況下,來自布局/示例的布局將被重用。

此處有更多詳細信息: https : //developer.android.com/training/improving-layouts/reusing-layouts.html#Include

暫無
暫無

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

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