简体   繁体   English

Android layout_below 未显示

[英]Android layout_below not showing

I am trying to position my RecyclerView so that it is below my AppBarLayout, however the layout_below attribute is not being recognised by android studio?我正在尝试 position 我的 RecyclerView 使其位于我的 AppBarLayout 下方,但是 android 工作室无法识别 layout_below 属性? It does not show in the auto-complete.它不会显示在自动完成中。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment.SearchFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bar"
        android:background="?android:attr/windowBackground">

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        android:id="@+id/toolbar">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_search_light"
        android:layout_marginRight="10dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search_bar"
        android:background="@android:color/transparent"
        android:hint="Search..."
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"/>

    </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_view" />

</FrameLayout>

Layout_Below exists in the relativeLayout, if you want to get access to that, you may need to switch to RelativeLayout or Alternativaly use linear layout if you want to put views one under the other by setting the orientation to vertical. Layout_Below 存在于 relativeLayout 中,如果你想访问它,你可能需要切换到 RelativeLayout 或者如果你想通过将方向设置为垂直来将视图放在另一个之下,则可能需要使用线性布局。

This is not correct.这是不正确的。 A RecyclerView should never go underneath an AppBarLayout. RecyclerView 不应该在 AppBarLayout 下方出现 go。 What you should do is ensure that there is sufficient margin between the RecyclerView and the AppbarLayout您应该做的是确保 RecyclerView 和 AppbarLayout 之间有足够的余量

Please change your layout as below.请更改您的布局如下。 Used LinearLayout with property android:orientation="vertical" instead of FrameLayout .使用带有属性android:orientation="vertical"而不是FrameLayoutLinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment.SearchFragment"
    android:orientation="vertical">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bar"
        android:background="?android:attr/windowBackground">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            android:id="@+id/toolbar">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_search_light"
                android:layout_marginRight="10dp"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/search_bar"
                android:background="@android:color/transparent"
                android:hint="Search..."
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"/>

        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_view" />

</LinearLayout>

you need the layout like ConstraintLayout, RelativeLayout and LinearLayout.你需要像 ConstraintLayout、RelativeLayout 和 LinearLayout 这样的布局。

example:例子:

ConstraintLayout:约束布局:

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


    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bar"
        android:background="?android:attr/windowBackground">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            android:id="@+id/toolbar">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_search_light"
                android:layout_marginRight="10dp"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/search_bar"
                android:background="@android:color/transparent"
                android:hint="Search..."
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"/>

        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        app:layout_constraintTop_toBottomOf="@id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_view" />


    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

RelativeLayout:相对布局:

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


    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bar"
        android:background="?android:attr/windowBackground">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            android:id="@+id/toolbar">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_search_light"
                android:layout_marginRight="10dp"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/search_bar"
                android:background="@android:color/transparent"
                android:hint="Search..."
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"/>

        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_below="@id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_view" />


    </RelativeLayout>

LinearLayout:线性布局:

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bar"
        android:background="?android:attr/windowBackground">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            android:id="@+id/toolbar">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_search_light"
                android:layout_marginRight="10dp"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/search_bar"
                android:background="@android:color/transparent"
                android:hint="Search..."
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"/>

        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recycler_view" />


    </LinearLayout>
 
    
    </FrameLayout>

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

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