繁体   English   中英

如何将2个布局彼此叠加

[英]how to put 2 layouts on top of each others

我正在尝试将2个布局彼此叠加,但它不起作用。有一个我不知道如何删除的空间。

这是我的代码:

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

使用相对布局将视图彼此重叠,像这样

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

现在,这也可以使用约束布局来完成。 在我的用例中,我有一个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>

在第二个LinearLayout ,您有一个android:layout_margin="25sp" 删除此行或至少不要设置marginTop。

暂无
暂无

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

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