【问题标题】:Android : layout_anchor was Not Showing in ConstraintLayoutAndroid:layout_anchor 未在 ConstraintLayout 中显示
【发布时间】:2020-08-03 15:56:37
【问题描述】:

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

app : layout_anchor="@+id/bottom_nav"

当我输入 xml 代码时显示此 layout_anchor,但之后我尝试添加 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>

【问题讨论】:

标签: android xml android-layout


【解决方案1】:

layout_anchorCoordinatorLayout 的属性,而不是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 与预构建工厂一起使用

【讨论】:

  • app:layout_constraintEnd_toEndOf="parent" bcoz 此行将按钮添加到父级的末尾,但我需要中心和搜索图标旁边
  • @Karthickyuvan 已编辑。根据需要更改边距底部以垂直移动
  • 浮动栏在 BottomNavigationView 后面,但我需要在前面
  • @Karthickyuvan 尝试在两个视图上玩 app:elevation
  • @Karthickyuvan 更改 android:layout_marginBottom
猜你喜欢
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
相关资源
最近更新 更多