Android 带权重的嵌套线性布局-如何在不同大小的设备上均匀分配空间
我有一个垂直的线性布局和许多水平的线性布局,占据了平板电脑的左侧 如果在嵌套布局上使用权重,则会收到有关嵌套权重的警告 我似乎也无法让事情看起来一致——我希望它们占据100%的屏幕大小 但是在一个小的5屏幕上,并不是所有的项目都显示出来,而在一个大的10屏幕上,它们只占屏幕的30% 我该怎么办 这是一个小平板电脑的外观,注意底部的项目被切断了 以下是10平板电脑上的外观,注意它不再占据完整的一面Android 带权重的嵌套线性布局-如何在不同大小的设备上均匀分配空间,android,android-layout,Android,Android Layout,我有一个垂直的线性布局和许多水平的线性布局,占据了平板电脑的左侧 如果在嵌套布局上使用权重,则会收到有关嵌套权重的警告 我似乎也无法让事情看起来一致——我希望它们占据100%的屏幕大小 但是在一个小的5屏幕上,并不是所有的项目都显示出来,而在一个大的10屏幕上,它们只占屏幕的30% 我该怎么办 这是一个小平板电脑的外观,注意底部的项目被切断了 以下是10平板电脑上的外观,注意它不再占据完整的一面 以下是一些建议: 对不同的屏幕大小使用备用布局。 您可以使用配置限定符来完成此操作。下面是一个示
以下是一些建议: 对不同的屏幕大小使用备用布局。 您可以使用配置限定符来完成此操作。下面是一个示例:您在/res/layout文件夹中有一个小屏幕的默认布局。现在,如果你想在更大的屏幕上有一个不同的布局,你可以创建一个不同的布局并把它放在一个不同的文件夹中;例如/res/layout-w600dp包含最小宽度为600dp的设备布局 请参阅有关Android开发者的本页: 要查看较小设备上的所有视图,请将布局包装在滚动视图中 然后,至少您的用户将能够滚动查看布局中的所有视图 要在较大的设备上垂直展开视图,请使用垂直视图之间带有权重的空间视图。 这是我用来在较大的设备上垂直展开视图的技巧。例如:
<LinearLayout
android:id="@+id/sidebar_linlay1"
android:layout_width="0dp"
android:layout_weight=".2"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address" />
<EditText
android:id="@+id/text_streetnum_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/text_streetname_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City" />
<EditText
android:id="@+id/text_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPostalAddress" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zip" />
<EditText
android:id="@+id/text_zip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email" />
<EditText
android:id="@+id/edittext_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:imeOptions="actionDone"
android:inputType="textEmailAddress" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type" />
<Spinner
android:id="@+id/spin_status"
android:layout_width="wrap_content"
android:padding="0dip"
android:layout_height="10dp" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:id="@+id/map_notes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:text="notes" />
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/but_ClientForm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Contract" />
<Button
android:id="@+id/but_saveData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Changes" />
</LinearLayout>
</LinearLayout>
空间视图将展开,以便视图在设备上均匀分布。这不是一颗神奇的子弹;您仍然需要查看视图在更大屏幕上的呈现方式,并找出它是否分布得过多。有时我会使用权重为1的空格,在最底部放置一个权重为2或3的空格。在更大的屏幕上,这会在底部留下一些空白,以便视图看起来不会太分散
嵌套权重警告只是让您知道,同时具有水平权重和垂直权重的布局渲染可能需要更长的时间。同样,你必须确定什么适合你。我不得不在我的一些布局上使用嵌套权重,并且没有任何不可接受的渲染延迟。但YMMV一如既往
<LinearLayout
android:id="@+id/sidebar_linlay1"
android:layout_width="0dp"
android:layout_weight=".2"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address" />
<EditText
android:id="@+id/text_streetnum_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/text_streetname_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City" />
<EditText
android:id="@+id/text_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPostalAddress" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zip" />
<EditText
android:id="@+id/text_zip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email" />
<EditText
android:id="@+id/edittext_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:imeOptions="actionDone"
android:inputType="textEmailAddress" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type" />
<Spinner
android:id="@+id/spin_status"
android:layout_width="wrap_content"
android:padding="0dip"
android:layout_height="10dp" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:id="@+id/map_notes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:text="notes" />
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/but_ClientForm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Contract" />
<Button
android:id="@+id/but_saveData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Changes" />
</LinearLayout>
</LinearLayout>