在 2 个布局之间浮动 textView

floating textView between 2 layout

提问人:Ratri 提问时间:7/16/2023 最后编辑:RF1991Ratri 更新时间:9/18/2023 访问量:23

问:

我有这样的设计,我想像图像一样将 2 个项目漂浮在中心,但我不知道该怎么做

enter image description here

我曾尝试过这样来实现那个设计,但仍然一无所获

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:background="@drawable/icon_banner_home">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:lineSpacingExtra="8dp"
                android:text="@string/selamat_datang_admin" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">

            <LinearLayout
                android:id="@+id/dashboard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="10dp"
                android:background="@drawable/rounded_corner_menu"
                android:orientation="vertical"
                android:padding="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_margin="10dp"
                    android:lineSpacingExtra="2dp"
                    android:text="90%"
                    android:textAlignment="center" />


                <TextView
                    android:layout_width="90dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:lineSpacingExtra="2dp"
                    android:text="Tugas Selesai Hari Ini"
                    android:textAlignment="center" />


            </LinearLayout>

            <LinearLayout
                android:id="@+id/checkin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_margin="10dp"
                android:layout_toRightOf="@+id/dashboard"
                android:background="@drawable/rounded_corner_menu"
                android:orientation="vertical"
                android:padding="10dp">

                <ImageView
                    android:layout_width="28dp"
                    android:layout_height="33dp"
                    android:layout_gravity="center"
                    android:src="@drawable/icon_green_checklist" />

                <TextView
                    android:layout_width="90dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:lineSpacingExtra="2dp"
                    android:text="Tepat Waktu"
                    android:textAlignment="center" />


            </LinearLayout>


        </RelativeLayout>

        <com.pastiberes.ExpandableHeightGridView
            android:id="@+id/gvMenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_marginTop="50dp"
            android:columnWidth="100dp"
            android:gravity="center"
            android:isScrollContainer="false"
            android:numColumns="4"
            android:scrollbars="none"
            android:stretchMode="columnWidth"
            android:textAlignment="center" />


    </LinearLayout>


</ScrollView>

我知道这是一个愚蠢的问题,我有搜索,但其中许多使用浮动按钮。你能帮帮我吗? 谢谢你的帮助

XML android-studio android-layout 浮动

评论


答:

0赞 Khush Parmar 9/18/2023 #1

您可以使用 来实现相同的目的,例如将一个父级设置为内部而不是 ,然后通过将仪表板 linearlayout 的 Constraint 顶部设置为标题的底部,以及设置相同的 bottom-to bottom。ConstraintLayoutConstraintLayoutScrollViewLinearLayout

下面是相同的示例,您可以根据需要对其进行修改。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:background="@drawable/icon_banner_home"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:lineSpacingExtra="8dp"
            android:text="@string/selamat_datang_admin" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@id/top"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/top"
        android:layout_gravity="center">

        <LinearLayout
            android:id="@+id/dashboard"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/rounded_corner_menu"
            android:orientation="vertical"
            android:padding="10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:lineSpacingExtra="2dp"
                android:text="90%"
                android:textAlignment="center" />


            <TextView
                android:layout_width="90dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:lineSpacingExtra="2dp"
                android:text="Tugas Selesai Hari Ini"
                android:textAlignment="center" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/checkin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_margin="10dp"
            android:layout_toRightOf="@+id/dashboard"
            android:background="@drawable/rounded_corner_menu"
            android:orientation="vertical"
            android:padding="10dp">

            <ImageView
                android:layout_width="28dp"
                android:layout_height="33dp"
                android:layout_gravity="center"
                android:src="@drawable/icon_green_checklist" />

            <TextView
                android:layout_width="90dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:lineSpacingExtra="2dp"
                android:text="Tepat Waktu"
                android:textAlignment="center" />

        </LinearLayout>

    </RelativeLayout>

    <com.pastiberes.ExpandableHeightGridView
        android:id="@+id/gvMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_marginTop="50dp"
        android:columnWidth="100dp"
        android:gravity="center"
        android:isScrollContainer="false"
        android:numColumns="4"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:textAlignment="center" />

</androidx.constraintlayout.widget.ConstraintLayout>