首页 > 解决方案 > Android:layout_anchor 未在 ConstraintLayout 中显示

问题描述

我想在我尝试使用的搜索图标旁边添加浮动图标加底部

app : layout_anchor="@+id/bottom_nav"

这个 layout_anchor 在我输入 xml 代码时显示,但我尝试在此之后添加 xml 代码,浮动栏也不在搜索图标之后

在此处输入图像描述

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add"
        
        app:backgroundTint="@color/colorPrimary"
         app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        /> </androidx.constraintlayout.widget.ConstraintLayout>

如果我删除这些行,它也会显示错误 this-view-is-not-constrained-it-only-has-designtime-positions-so-it-will-jump to.....

            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"

bottom_nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="@string/home_menu"
    android:id="@+id/home_menu"
    android:icon="@drawable/ic_home"
    />
    <item android:title="@string/search_menu"
        android:id="@+id/search_menu"
        android:icon="@drawable/ic_search"
        />

    <item android:title="@string/list_menu"
        android:id="@+id/list_menu"
        android:icon="@drawable/ic_filter_list"
        />
    <item android:title="@string/settings_menu"
        android:id="@+id/setting_menu"
        android:icon="@drawable/ic_settings"
        />
</menu>

标签: androidxmlandroid-layout

解决方案


layout_anchorCoordinatorLayoutnot的一个属性ConstraitLayout

试试这个

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:menu="@menu/bottom_nav_menu" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:src="@drawable/ic_add"
    app:elevation="100dp"
    app:backgroundTint="@color/colorPrimary"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>

BottomAppBar与预建工厂一起使用


推荐阅读