简体   繁体   English

带权重的嵌套线性布局-如何在不同尺寸的设备上均匀间隔

[英]nested linear layouts with weights - how to space evenly on different size devices

I have a vertical linear layout with many linear layouts (horizontal) that take up the left side of aa 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) 这是一台小型平板电脑上的外观(注意,下面的项目已被切除) 5“版本

Here is what it looks like on a 10" tablet (notice it no longer takes up the full side) 这是10英寸平板电脑上的外观(注意,它不再占据整个侧面) 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>

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. 这是一个示例: /res/layout文件夹中的小屏幕具有默认布局。 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-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. 要在较大的设备上垂直分布视图,请使用Space视图,权重介于垂直视图之间。

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 Space s 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的Space ,并在最底部使用权重为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一如既往。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM