簡體   English   中英

Android layout_below 未顯示

[英]Android layout_below not showing

我正在嘗試 position 我的 RecyclerView 使其位於我的 AppBarLayout 下方,但是 android 工作室無法識別 layout_below 屬性? 它不會顯示在自動完成中。

<?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 存在於 relativeLayout 中,如果你想訪問它,你可能需要切換到 RelativeLayout 或者如果你想通過將方向設置為垂直來將視圖放在另一個之下,則可能需要使用線性布局。

這是不正確的。 RecyclerView 不應該在 AppBarLayout 下方出現 go。 您應該做的是確保 RecyclerView 和 AppbarLayout 之間有足夠的余量

請更改您的布局如下。 使用帶有屬性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>

你需要像 ConstraintLayout、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">
    <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>

相對布局:

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

線性布局:

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