Android 浮动按钮不在右下角浮动,带有后参考
进入安卓系统30个小时,现在进入更酷的领域。我试着跟随这篇文章 我的代码如下所示Android 浮动按钮不在右下角浮动,带有后参考,android,android-studio,material-design,floating-action-button,material-components-android,Android,Android Studio,Material Design,Floating Action Button,Material Components Android,进入安卓系统30个小时,现在进入更酷的领域。我试着跟随这篇文章 我的代码如下所示 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="h
<?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=".base.ActivityContactList">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/underline"
android:scrollbars="vertical"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16px"
android:src="@drawable/plus_icon3"
app:fabSize="normal"
app:layout_anchor="@id/contact_list"
app:layout_anchorGravity="bottom|right|end" />
</androidx.constraintlayout.widget.ConstraintLayout>
我创建了图标视图“新建->图像资源”,然后选择带有主题自定义的clipart,以便设置颜色
如果使用的是ConstraintLayout
,则可以执行以下操作:
到浮动操作按钮
如果使用的是ConstraintLayout
,则可以执行以下操作:
到浮动操作按钮
我的图标不在右下角
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
android:layout_margin="16px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/ic_baseline_home_24"
app:fabSize="normal" />
mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));
由于使用的是ConstraintLayout
,因此需要设置所需的约束。就你而言
layout_constraintBottom_toBottomOf="parent"
layout_constraintEnd_toEndOf="parent"
因此,您的浮动操作按钮将变为
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/ic_baseline_home_24"
app:fabSize="normal" />
我的图标不在右下角
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimary"
android:layout_margin="16px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/ic_baseline_home_24"
app:fabSize="normal" />
mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));
由于使用的是ConstraintLayout
,因此需要设置所需的约束。就你而言
layout_constraintBottom_toBottomOf="parent"
layout_constraintEnd_toEndOf="parent"
因此,您的浮动操作按钮将变为
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/ic_baseline_home_24"
app:fabSize="normal" />
使用类似于:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/..." />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
关于颜色,请确保:
- 使用最新稳定的
实现'com.google.android.material:material:1.1.0'
- 使用
主题。MaterialComponents.*
作为应用程序主题
使用类似于:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/..." />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
关于颜色,请确保:
- 使用最新稳定的
实现'com.google.android.material:material:1.1.0'
- 使用
主题。MaterialComponents.*
作为应用程序主题
使用坐标系布局代替约束图使用坐标系布局使用坐标系布局默认背景色为colorSecondary
而不是colorAccent
@GabrieleMariotti文档中这么说。我通过运行它再次检查了这一点。默认的背景颜色是colorSecondary
而不是colorAccent
@GabrieleMariotti文档中这么说的。我通过运行它再次检查了这一点。默认背景颜色是colorSecondary
而不是colorAccent
默认情况下,晶圆厂由colorAccent
属性着色,请检查。不仅如此。在“材质组件”主题中,您不应使用accentColor。默认背景色为colorSecondary
而不是colorAccent
。默认情况下,晶圆厂由colorAccent
属性着色,请选中。不仅如此。在Material Components主题中,你不应该使用accentColor。我使用的是约束布局,因此下面的答案比CoordinatorLayout更适合我。然而,你关于主题的提示,天哪,太棒了!我不知道它的存在。我现在正在努力玩,还不知道他们在文档中引用的是什么文件,但希望我能做到!!谢谢@DeanHiller你可以开始Mariotte lol,我在那个页面上,但它从来没有提到文件名,所以我必须搜索内容。这是一个很难理解的教程(对于新手来说),但最后,我意识到android studio 4实际上为我做了所有这些,但它让我对android开发的主题方面有了一些非常好的了解,这非常好。我使用了约束布局,所以下面的答案比协调布局更适合我,你的主题提示,天哪,太棒了!我不知道它的存在。我现在正在努力玩,还不知道他们在文档中引用的是什么文件,但希望我能做到!!谢谢@DeanHiller你可以开始Mariotte lol,我在那个页面上,但它从来没有提到文件名,所以我必须搜索内容。这是一个很难理解的教程(对于新手来说),但最后,我意识到android studio 4实际上为我做了所有这些,但它让我对android开发的主题方面有了一些非常好的了解,这非常好。