Android - 避免布局重复

时间:2015-08-20 16:27:12

标签: android xml android-layout layout dpi

我有一个名为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>

原始布局文件(上图)显示了带有“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>

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

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

是否可以在公共UI元素中使用单个“基本布局”,并在当前屏幕尺寸上相应地包含较小的布局更改?

2 个答案:

答案 0 :(得分:2)

尝试制作资源文件,例如。 values-sw600dp/strings.xmlvalues-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>

答案 1 :(得分:0)

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

我们称之为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>

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

相关问题