[英]android put button at edge of view
您说您正在使用约束布局,因此您可以将按钮设置在该灰色背景视图的右下角。
在视图的 XML 中,确保按钮位于 XML 文件中的灰色视图之后,并设置按钮的约束布局属性(我已经为 layout_width 和 layout_height 指定了任意值:
...
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/backrgoundGreyView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="@id/headerMenuButton"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="20dp"
android:layout_height="20dp"
app:layout_constraintStart_toEndOf="@id/backgroundGreyView"
app:layout_constraintEnd_toEndOf="@id/backgroundGreyView"
app:layout_constraintTop_toBottomOf="@id/backgroundGreyView"
app:layout_constraintBottom_toBottomOf="@id/backgroundGreyView"/>
...
您可以使用属性: layout_anchor
和layout_anchorGravity
但它仅适用于CoordinatorLayout
样本:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/imageView"
app:layout_anchorGravity="end|bottom" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
它看起来像:
输出:代码输出
布局文件:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#255177">
<!--Image code-->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/round_box"
android:src="@drawable/demo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="@id/imageView"
app:layout_constraintEnd_toEndOf="@id/imageView"
app:layout_constraintStart_toEndOf="@id/imageView"
app:layout_constraintTop_toBottomOf="@id/imageView">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:background="@mipmap/edit_pro" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
您可以将两者都包装在ConstraintLayout
并操纵对 fab 顶部和底部的约束。
但这还不足以让按钮参与图像,因此,您可以通过使用android:scaleX
和android:scaleY
缩放 FAB 和/或图像来添加处理,并使用app:fabSize="mini"
将 FAB 大小设置为 mini app:fabSize="mini"
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0E153A">
<ImageView
android:id="@+id/image"
android:layout_width="130dp"
android:layout_height="130dp"
android:background="@drawable/rect_frame"
android:scaleX="1.1"
android:scaleY="1.1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="40dp"
android:layout_height="wrap_content"
android:backgroundTint="#3D5AF1"
app:fabSize="mini"
app:borderWidth="0dp"
app:layout_constraintBottom_toBottomOf="@id/image"
app:layout_constraintEnd_toEndOf="@id/image"
app:layout_constraintStart_toEndOf="@id/image"
app:layout_constraintTop_toBottomOf="@+id/msimageg"
app:srcCompat="@drawable/ic_baseline_edit_24"
app:tint="#FFF" />
</androidx.constraintlayout.widget.ConstraintLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.