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

Android 为什么第一个亲戚会占据整个屏幕,而不是与所有元素共享空间?,android,android-layout,android-xml,android-layout-weight,Android,Android Layout,Android Xml,Android Layout Weight,我想这样做: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- list of subviews --> </LinearLayout> <Button android:layout_width="0dp"

我想这样做:

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

    <!-- list of subviews -->

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

首先,我不想要滚动视图

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

所以,最好的工作方式就是减肥。当我在所有亲戚中添加权重时,第一个亲戚会增长很多,占据所有屏幕,而不是与所有元素共享空间

这是我的密码

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

正如我看到的,您将相同的值设置为all
RelativeLayout
,这样它们将占用相同的空间量,但这并不意味着它们会小两倍

重量并不意味着什么秤;-)

如果您想在图像上看到一个视图组,但在。。。请仔细阅读:

以下是我一直以来最喜欢的关于
布局重量的解释
如果有多个视图跨越
线性布局
,则
layout\u weight
为它们提供了一个比例大小。风景 越大的
layou-weight
value“权重”越大,所以它得到的空间越大

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

理论 “布局权重”一词与数学中的“布局权重”概念有关。就像在大学课堂上,家庭作业 价值30%,出席率10%,期中考试价值20%,以及 最终的价值是40%。加权后,这些部分的得分 一起给你总成绩

布局权重也是如此。
以水平方向查看
LinearLayout
每个都可以占总布局的一定百分比 宽度。(或垂直
线性布局的高度百分比

布局 您使用的
线性布局将如下所示:

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

    <!-- list of subviews -->

</LinearLayout>
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />
请注意,您需要将
layout\u width=“0dp”
layout\u weight=“1”
。忘记这一点会导致许多新用户出现问题。 (请参阅,以了解通过不使用 将宽度设置为0。)如果视图处于垂直方向
LinearLayout
然后您当然会使用
layout\u height=“0dp”

在上面的
按钮
示例中,我将权重设置为1,但您可以使用 任何号码。重要的只是总数。你可以在地图上看到 我发布的第一张图片中有三行按钮,数字 都不一样,但由于比例相同 加权宽度不会在每行中更改。有些人喜欢使用 总和为1的十进制数,因此在复杂布局中 明确每个零件的重量

最后一个音符。如果您有许多嵌套布局使用
layout\u 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>

如果您有任何问题,请随时提问


希望有帮助

尝试在包含子视图的父视图中添加
android:weight\u sum=“10”
,然后使用
layout\u weight
为它们提供比例

您的错误很简单-您只为亲属添加了权重。此外,同样的值,你似乎像scaleXY一样考虑重量。阅读下面我的答案。任何qestion高鉴赏力我阅读了你建议的所有内容,我理解,并且我确定了一种可行的方法,但它正在增加第一个元素,而不是改进:(更新你的代码,我会在我的Android Studio和设备上测试它。别担心:-)我把它放在了上面,但它仍然在所有屏幕上显示第一个相对的元素:(