如何在列表视图顶部设置选项卡布局但不覆盖Android?

How to set tab layout on top of listview but not overlaying in Android?

大家都在。 我想请问有人可以帮我解决这个问题。 我希望我的 ListView 显示在选项卡布局的底部。

这是我的 xml 文件。 ListView 在选项卡布局后面。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    android:orientation="vertical"
    android:background="@color/bone"
    tools:context=".AddNewAdmin">

    <ProgressBar
        android:id="@+id/loadingN"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="8dp"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolBar" />

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolBar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            app:popupTheme="@style/Widget.AppCompat.PopupMenu.Overflow"/>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabUsers"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:background="@color/morandi_white"
            android:theme="@style/ThemeOverlay.AppCompat"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </com.google.android.material.appbar.AppBarLayout>

    <include
        layout="@layout/content_main"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="20dp"
        android:src="@android:drawable/ic_input_add"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:contentDescription="add" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/mViewpager_ID"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</androidx.constraintlayout.widget.ConstraintLayout>

显示如下。 Click here

还有一种情况是ListView在tab布局的前面,无法点击

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    android:orientation="vertical"
    android:background="@color/bone"
    tools:context=".AddNewAdmin">

    <ProgressBar
        android:id="@+id/loadingN"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="8dp"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolBar" />

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolBar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            app:popupTheme="@style/Widget.AppCompat.PopupMenu.Overflow"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabUsers"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:background="@color/morandi_white"
            android:theme="@style/ThemeOverlay.AppCompat"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    <include
        layout="@layout/content_main"
        app:layout_constraintTop_toBottomOf="@id/tabUsers"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="20dp"
        android:src="@android:drawable/ic_input_add"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:contentDescription="add" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/mViewpager_ID"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</androidx.constraintlayout.widget.ConstraintLayout>

显示为this picture.

感谢您的阅读。感谢任何帮助。

您必须使用更多约束才能正确定位元素。

给出的例子:

        <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        app:popupTheme="@style/Widget.AppCompat.PopupMenu.Overflow"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabUsers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:background="@color/morandi_white"
        android:theme="@style/ThemeOverlay.AppCompat"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

这 2 个元素都在使用约束 Top_toTopOf="parent" 所以它们重叠,因为它们都对齐到同一个 ConstraintLayout 的顶部。

最好的方法是开始将第一个视图 (=element1) 定位在 ConstraintLayout (Top_toTopOf="parent") 的顶部,然后将下面的视图 (=element2) 与

对齐
Top_toBottomOf="@id/element1"

Element3 将在 element2 下与

对齐
Top_toBottomOf="@id/element2"

您还可以使用更多的 ConstraintLayouts。

/> 是 xml-Layout 的结束标签,如果您想在 Layout 中放置一个 View,结束标签必须位于 View 元素之后。

如果是:

  <com.google.android.material.tabs.TabLayout
    android:id="@+id/tabUsers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dp"
    android:background="@color/morandi_white"
    android:theme="@style/ThemeOverlay.AppCompat"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

任何后续视图元素都将位于同一层次结构中的此布局之外。