如何将两个布局叠加在一起?

6

我想把两个布局放在一起,但是它们之间有一个无法删除的空间。

这是我的代码:

<LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical" 
                android:layout_gravity="bottom|center"
                android:weightSum="1">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="170dp"
                    android:layout_weight="0.5"
                    android:background="@drawable/column_white"
                    android:layout_gravity="bottom|center"
                    android:gravity="bottom"
                    android:orientation="vertical"

                    >
                    <LinearLayout 
                        android:layout_width="10sp"
                        android:layout_height="100sp"
                        android:background="@drawable/arrow_body"
                        android:layout_marginLeft="10sp"
                        ></LinearLayout>
                    <LinearLayout 
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:background="@drawable/arrow_shape"
                        android:layout_marginLeft="6sp"
                        ></LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="150dp"
                    android:layout_margin="25sp"
                    android:background="@drawable/column_blue"
                    android:gravity="center"
                    android:layout_weight="0.5" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="40 000 "
                        android:textColor="#ffffff"
                        android:textSize="25sp" />
                </LinearLayout>

            </LinearLayout>

使用 RelativeLayout

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="170dp"
        android:background="@drawable/column_white"
        android:gravity="bottom"

        android:layout_alignBottom="@id/second_part"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="10sp"
            android:layout_height="100sp"
            android:layout_marginLeft="10sp"
            android:background="@drawable/arrow_body" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6sp"
            android:background="@drawable/arrow_shape" >
        </LinearLayout>

    </LinearLayout>

    <LinearLayout

        android:id="@+id/second_part"
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:layout_margin="25sp"

        android:background="@drawable/column_blue"
        android:gravity="center" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="40 000"
            android:textColor="#ffffff"
            android:textSize="25sp" />
    </LinearLayout>
</RelativeLayout>

3
使用相对布局(Relative Layout)作为根视图。 - TheFlash
请使用RelativeLayout或FrameLayout作为根布局。 - Software Sainath
我也尝试过了,修改了我的代码。 - user2137817
你能给我们展示截图吗? - GrIsHu
1
请阅读我的评论。在您的相对布局中,第二个仍然有25sp的边距。 - Damien R.
这个答案可能对你有帮助:https://dev59.com/Hmw15IYBdhLWcg3wT6H2#6690607 - amin
3个回答

8
使用相对布局将视图放置在彼此之上,就像这样:
 <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:background="#f00"
        android:orientation="vertical"
        android:weightSum="1" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="170dp"
            android:layout_centerInParent="true"
            android:layout_gravity="bottom|center"
            android:background="#fff"
            android:gravity="bottom"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="10dp"
                android:layout_height="100sp"
                android:layout_centerInParent="true"
                android:layout_marginLeft="10sp"
                android:background="#37c100" >
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="6sp"
                android:background="@drawable/arrow_shape" >
            </LinearLayout>
        </RelativeLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_centerInParent="true"
            android:layout_margin="25sp"
            android:layout_weight="0.5"
            android:background="#0000ff"
            android:gravity="center" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="40 000 "
                android:textColor="#ffffff"
                android:textSize="25sp" />
        </LinearLayout>
    </RelativeLayout>

-1

现在也可以使用ConstraintLayout来完成这个操作。在我的使用案例中,我有一个RecyclerView,当它为空时,必须用文本和按钮“覆盖”它,以允许用户向列表中添加项目。(来自《大牛书》编程书籍的挑战13.3的一部分)

代码如下(根据您自己的需求进行更改):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/no_crimes_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/no_crimes_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="@string/no_crimes"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/recyclerView"
            app:layout_constraintStart_toStartOf="@+id/recyclerView"
            app:layout_constraintTop_toTopOf="@+id/recyclerView" />

        <Button
            android:id="@+id/new_crime_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="@string/new_crime"
            app:layout_constraintBottom_toBottomOf="@+id/recyclerView"
            app:layout_constraintEnd_toEndOf="@+id/recyclerView"
            app:layout_constraintStart_toStartOf="@+id/recyclerView"
            app:layout_constraintTop_toBottomOf="@+id/textView" />

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/crime_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>

1
你没有解释为什么这个能够工作。在我看来,RecyclerView 应该会位于 LinearLayout 的上方,因为它在层次结构中排在后面。 - Brill Pappin

-1
在你的第二个 LinearLayout 中,你设置了 android:layout_margin="25sp"。 请移除这行代码或者至少不要设置 marginTop。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接