首页 > 解决方案 > 如何在约束布局中添加浮动按钮

问题描述

错误说

“Resource Errors 该项目包含资源错误,所以aapt没有成功,可能会导致渲染失败。先修复资源问题。”

如何在约束布局中添加浮动操作按钮

这是我的代码

<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_wishlist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:src="@drawable/ic_heart"
        app:backgroundTint="@color/cardview_dark_background"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.972"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent />


</androidx.constraintlayout.widget.ConstraintLayout>

标签: javaandroid

解决方案


尝试这个!

<?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"
    tools:context=".MainActivity">


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/abc_vector_test" />
    
</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读