【发布时间】: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