【问题标题】:Android - Avoid layout duplicationAndroid - 避免布局重复
【发布时间】:2015-08-20 16:27:11
【问题描述】:

我有一个名为 visit_registration.xml 的巨大布局文件。为了支持不同的屏幕尺寸,我复制了文件并创建了文件 layout-sw600dp/visit_registration.xml 和 layout-sw720dp/visit_registration.xml。

布局之间有一个小的区别:

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

    <TextView
        android:id="@+id/visitRegistrationTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/visit_registration" />

    <!-- TONS of UI elements here! -->
</LinearLayout>

原始布局文件(上图)显示了 ID 为“visitRegistrationTextView”的 TextView。

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

    <TextView
        android:id="@+id/visitRegistrationTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:text="@string/visit_registration" />

    <!-- TONS of UI elements here! -->
</LinearLayout>

每个副本使用不同的可见性。上面的例子使用了“gone”。有时变化是不同的,但很小,就像 UI 元素移动到布局中的另一个位置一样。其余代码保持不变。

主要问题是关于布局复制。我不想维护三个不同的布局,因为经典的重复问题,例如“如果我需要更改三个文件中存在的通用 UI 元素并忘记在一个地方更改它会发生什么?”

是否可以有一个带有通用 UI 元素的单一“基本布局”,并根据当前屏幕尺寸在其上包含相应的小布局更改?

【问题讨论】:

    标签: android xml android-layout layout dpi


    【解决方案1】:

    尝试制作资源文件,例如。 values-sw600dp/strings.xml, values-sw720dp/strings.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="visibility">gone</string>
    </resources>
    

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="visibility">visible</string>
    </resources>
    

    和你的布局文件(只有一个):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingLeft="@dimen/app_margin"
        android:paddingRight="@dimen/app_margin">
    
        <TextView
            android:id="@+id/visitRegistrationTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="@string/visibility"
            android:text="@string/visit_registration" />
    
        <!-- TONS of UI elements here! -->
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      您可以使用单个文本视图定义布局,每个文本视图对应您的屏幕尺寸。然后,您创建一个可以在其中包含以前的总体布局。

      我们称之为text_layout,每个屏幕尺寸一个。

      <LinearLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical">
      
          <TextView
              android:id="@+id/visitRegistrationTextView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:visibility="gone"
              android:text="@string/visit_registration" />
      </LinearLayout>
      

      然后您创建主布局,一种适用于所有屏幕尺寸的布局。

      <LinearLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:paddingLeft="@dimen/app_margin"
          android:paddingRight="@dimen/app_margin">
      
          <include layout="@layout/text_layout"/>
      
          <!-- TONS of UI elements here! -->
      
      </LinearLayout>
      

      就是这样,更少的重复,更多的维护。

      【讨论】:

      • 根据您的建议,我是否需要为每个更改创建一个文件并将其包含在主布局中?
      • 是的,没错。您必须在某个地方定义各种配置的差异。
      • 但是我可以在单个文件中定义差异吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多