嵌套线性布局与权重 - 如何在不同大小的设备上均匀分布

时间:2023-02-03 10:12:05

I have a vertical linear layout with many linear layouts (horizontal) that take up the left side of a a tablet

我有一个垂直的线性布局,有很多线性布局(水平),占据了平板电脑的左侧

I get warnings about nesting weights if I use weights on the nested layouts

如果我在嵌套布局上使用权重,我会收到有关嵌套权重的警告

I also can't seem to get things to look consistent - I want them to take up 100% of the screen size

我似乎也无法让事情看起来一致 - 我希望它们占据屏幕尺寸的100%

but on a small 5" screen not all the items are shown, and on a large 10" screen they only take up a 30% of the screen

但是在一个小的5英寸屏幕上并没有显示所有项目,而在10英寸的大屏幕上它们只占据屏幕的30%

What am suppose to do?

我想做什么?

Here is what it looks like on a small tablet (notice the bottom items are cut off) 嵌套线性布局与权重 - 如何在不同大小的设备上均匀分布

这是小型平板电脑的样子(注意底部物品被切断)

Here is what it looks like on a 10" tablet (notice it no longer takes up the full side) 嵌套线性布局与权重 - 如何在不同大小的设备上均匀分布

这是10英寸平板电脑的样子(注意它不再占据整面)

<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>

            <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>

            <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>

            <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>

            <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>

            <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>



            <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" />

            <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 个解决方案

#1


0  

Here are a couple suggestions:

以下是一些建议:

  • Use alternate layouts for different screen sizes.
  • 对不同的屏幕尺寸使用备用布局。

You do this with configuration qualifiers. Here is an example: You have a default layout for small screens in /res/layout folder. Now, if you want to have a different layout for a larger screen, you create a variation of your layout and put it in a different folder; for example /res/layout-w600dp contains layouts for devices with a minimum width of 600dp.

您可以使用配置限定符执行此操作。下面是一个示例:/ res / layout文件夹中有小屏幕的默认布局。现在,如果您希望为更大的屏幕设置不同的布局,则可以创建布局的变体并将其放在不同的文件夹中;例如/ res / layout-w600dp包含最小宽度为600dp的设备的布局。

Refer to this page on Android Developers:

请参阅Android开发者的此页面:

Supporting Multiple Screens | Android Developers

支持多个屏幕| Android开发者

  • To see all the views on a smaller device, wrap your layout in a ScrollView
  • 要在较小的设备上查看所有视图,请将您的布局包装在ScrollView中

Then at least your users will be able to scroll to see all the views in your layout.

然后,至少您的用户将能够滚动以查看布局中的所有视图。

  • To spread out views vertically on larger devices, use Space view with weights in between vertical views.
  • 要在较大的设备上垂直展开视图,请使用垂直视图之间具有权重的空间视图。

This is a trick I use to spread views out vertically on larger devices. For example:

这是我用来在大型设备上垂直传播视图的技巧。例如:

<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>

The Space views will expand so that the views are spread out evenly on the device. This is not a magic bullet; you still have to look at how the view renders on larger screens and find out if it's spread out too much. Sometimes I will use Spaces with weight of 1 and at the very bottom, put a Space with a weight of 2 or 3. On larger screens, this leaves some margin at the bottom so that the views don't look too spread out.

空间视图将展开,以便视图在设备上均匀分布。这不是一个神奇的子弹;你仍然需要看看视图如何在更大的屏幕上呈现,并找出它是否分散得太多。有时我会使用重量为1的Spaces,在最底部放置一个重量为2或3的Space。在较大的屏幕上,这会在底部留下一些边距,这样视图看起来就不会太分散了。

The nested weights warning is just to let you know that layouts having both horizontal and vertical weights may take longer to render. Again, you have to determine what works for you. I have had to resort to nested weights on some of my layouts, and I haven't had any unacceptable rendering delays. But YMMV as always.

嵌套权重警告只是为了让您知道具有水平和垂直权重的布局可能需要更长时间才能呈现。同样,你必须确定哪些对你有用。我不得不在我的一些布局上使用嵌套权重,并且我没有任何不可接受的渲染延迟。但YMMV一如既往。

#1


0  

Here are a couple suggestions:

以下是一些建议:

  • Use alternate layouts for different screen sizes.
  • 对不同的屏幕尺寸使用备用布局。

You do this with configuration qualifiers. Here is an example: You have a default layout for small screens in /res/layout folder. Now, if you want to have a different layout for a larger screen, you create a variation of your layout and put it in a different folder; for example /res/layout-w600dp contains layouts for devices with a minimum width of 600dp.

您可以使用配置限定符执行此操作。下面是一个示例:/ res / layout文件夹中有小屏幕的默认布局。现在,如果您希望为更大的屏幕设置不同的布局,则可以创建布局的变体并将其放在不同的文件夹中;例如/ res / layout-w600dp包含最小宽度为600dp的设备的布局。

Refer to this page on Android Developers:

请参阅Android开发者的此页面:

Supporting Multiple Screens | Android Developers

支持多个屏幕| Android开发者

  • To see all the views on a smaller device, wrap your layout in a ScrollView
  • 要在较小的设备上查看所有视图,请将您的布局包装在ScrollView中

Then at least your users will be able to scroll to see all the views in your layout.

然后,至少您的用户将能够滚动以查看布局中的所有视图。

  • To spread out views vertically on larger devices, use Space view with weights in between vertical views.
  • 要在较大的设备上垂直展开视图,请使用垂直视图之间具有权重的空间视图。

This is a trick I use to spread views out vertically on larger devices. For example:

这是我用来在大型设备上垂直传播视图的技巧。例如:

<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>

The Space views will expand so that the views are spread out evenly on the device. This is not a magic bullet; you still have to look at how the view renders on larger screens and find out if it's spread out too much. Sometimes I will use Spaces with weight of 1 and at the very bottom, put a Space with a weight of 2 or 3. On larger screens, this leaves some margin at the bottom so that the views don't look too spread out.

空间视图将展开,以便视图在设备上均匀分布。这不是一个神奇的子弹;你仍然需要看看视图如何在更大的屏幕上呈现,并找出它是否分散得太多。有时我会使用重量为1的Spaces,在最底部放置一个重量为2或3的Space。在较大的屏幕上,这会在底部留下一些边距,这样视图看起来就不会太分散了。

The nested weights warning is just to let you know that layouts having both horizontal and vertical weights may take longer to render. Again, you have to determine what works for you. I have had to resort to nested weights on some of my layouts, and I haven't had any unacceptable rendering delays. But YMMV as always.

嵌套权重警告只是为了让您知道具有水平和垂直权重的布局可能需要更长时间才能呈现。同样,你必须确定哪些对你有用。我不得不在我的一些布局上使用嵌套权重,并且我没有任何不可接受的渲染延迟。但YMMV一如既往。