为什么第一个亲戚占据了整个屏幕,而不是与所有元素共享空间?

时间:2015-12-28 21:35:24

标签: android android-layout android-xml android-layout-weight

我想做这样的事情:

首先,我不想要滚动视图。

第二点是:我需要一个固定的页脚。

所以,最好的工作方式是体重。当我把重量放在所有亲戚身上时,第一个亲戚占据了整个屏幕,而不是与所有元素共享空间。

这是我的代码。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:fitsSystemWindows="true"
        android:background="@drawable/bg_app"
        tools:context=".ui.home.HomeActivity">
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/app_bar"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/green_tataki"
                android:padding="5dp"
                app:popupTheme="@style/AppTheme.PopupOverlay">
                <!--Here the action bar-->
            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.AppBarLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/app_bar"
            android:orientation="vertical">
            <View
                android:layout_width="wrap_content"
                android:layout_height="4dp"
                android:layout_above="@+id/image_product"
                android:background="@drawable/bg_transparent_header"
                android:backgroundTintMode="multiply"/>
            <!--Relative with background as image-->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:id="@+id/image_product"
                android:background="@drawable/sushi">
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/image_product"
                android:id="@+id/box_description"
                android:background="@color/white"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp">
                <!--Text view inside relative-->
            </RelativeLayout>

            <!--Relative 1-->
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:layout_below="@+id/box_description"
                android:id="@+id/quantity_button"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="3dp"
                android:background="@drawable/border_left">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="RELATIVE 1"/>
            </RelativeLayout>

            <!--Relative 2-->
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="3dp"
                android:id="@+id/additional_button"
                android:layout_below="@+id/quantity_button"
                android:background="@drawable/border_left">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="RELATIVE 1"/>
            </RelativeLayout>
            <!--Relative 3-->
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="3dp"
                android:id="@+id/delete_button"
                android:layout_below="@+id/additional_button"
                android:background="@drawable/border_left">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="RELATIVE 3"/>
            </RelativeLayout>
            <!--Relative 4-->
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.5"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_below="@+id/delete_button"
                android:background="@drawable/border_left"
                android:id="@+id/clarification_button">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="RELATIVE 4"
                        />
            </RelativeLayout>
            <!--Button-->
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.5"
                    android:gravity="center"
                    android:layout_margin="10dp"
                    android:padding="5dp"
                    android:id="@+id/button_add_to_car"
                    android:textColor="@color/brown_tataki"
                    android:layout_below="@+id/clarification_button"
                    android:background="@color/green_tataki"
                    android:text="@string/add_to_car"
                    android:textSize="15sp"/>
        </LinearLayout>
    <!--Footer-->
        <View
            android:layout_width="wrap_content"
            android:layout_height="4dp"
            android:layout_above="@+id/footer"
            android:background="@drawable/bg_transparent_footer"
            android:backgroundTintMode="multiply"/>


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/green_tataki"
            android:layout_alignParentBottom="true"
            android:gravity="bottom"
            android:id="@+id/footer"
            android:padding="5dp">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RELATIVE 4"/>
        </RelativeLayout>
    </RelativeLayout>
<!--Menu-->
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="@color/white"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_gravity="right"
        android:choiceMode="singleChoice"
        android:dividerHeight="0.5dp"
        android:background="@color/brown_tataki"
        android:id="@+id/left_drawer"/>
</android.support.v4.widget.DrawerLayout>

2 个答案:

答案 0 :(得分:0)

我看到你为所有RelativeLayout设置了相同的值,所以它们会占用相同的空间,但这并不意味着它会小两倍

重量并不代表相同的比例; - )

如果你想在图像上看到ViewGroup,你应该在所有视图中添加权重参数,但在......之前请仔细阅读:What does android:layout_weight mean?

以下是我layout_weight

最喜欢的解释
  

如果有多个视图跨越LinearLayout,那么   layout_weight给每个人一个比例大小。一个视图与   更大layout_weight值&#34;重量&#34;更多,所以它有更大的空间。

     

这是一张让事情更清晰的图片。

     

enter image description here

     

理论

     

术语布局权重与数学中weighted average的概念有关。就像在大学课上做作业一样   值30%,出勤率10%,中期值20%,而且   决赛价值40%。加权后您对这些部分的分数   在一起,给你你的总成绩。

     

enter image description here

     

布局重量相同。横向Views   每个LinearLayout可以占总数的一定比例   宽度。 (或垂直LinearLayout的高度的百分比。)

     

布局

     

您使用的LinearLayout将如下所示:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <!-- list of subviews -->

</LinearLayout>
     

请注意,您必须使用layout_width="match_parent"   LinearLayout。如果您使用wrap_content,那么它将无法正常工作。也   请注意layout_weight不适用于中的视图   RelativeLayouts(请参阅herehere了解SO答案   这个问题)。

     

视图

     

水平LinearLayout中的每个视图都如下所示:

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />
     

请注意,您需要同时使用layout_width="0dp"   layout_weight="1"。忘记这会导致许多新用户出现问题。   (有关您可以获得的不同结果,请参阅this article   将宽度设置为0.)如果您的视图位于垂直中   LinearLayout然后你会使用layout_height="0dp"

     

在上面的Button示例中,我将权重设置为1,但您可以使用   任何数字。重要的只是总数。你可以在里面看到   我发布的第一张图片中的三行按钮,数字   都是不同的,但由于比例是相同的,所以   加权宽度不会在每一行中发生变化。有些人喜欢用   总和为1的十进制数,以便在复杂的布局中   清楚每个部分的重量是什么。

     

最后一点说明。如果您有许多使用的嵌套布局   layout_weight,这可能对性能不利。

     

附加

     

以下是顶部图片的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android:layout_weight="
        android:textSize="24sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="2" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android:layout_weight="
        android:textSize="24sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:text="10" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="20"
            android:text="20" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:text="10" />

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android:layout_weight="
        android:textSize="24sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text=".25" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".50"
            android:text=".50" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text=".25" />

    </LinearLayout>

</LinearLayout>

如果您有任何疑问,请随意提问。

希望有所帮助

答案 1 :(得分:0)

尝试在包含子视图的父视图中添加android:weight_sum="10",然后使用layout_weight为其提供比率