簡體   English   中英

android將按鈕放在視圖邊緣

[英]android put button at edge of view

示例視圖

我正在嘗試在image view的角落添加一個floating action button 我已經查看了關於 stackoverflow 的所有當前解決方案,但我遇到的答案並不詳細,只包含廣泛的信息,例如“使用布局錨點”或“使用框架布局”。 我該怎么做呢? 順便說一下,我使用的根布局是一個constraint layout

您說您正在使用約束布局,因此您可以將按鈕設置在該灰色背景視圖的右下角。

在視圖的 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_anchorlayout_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:scaleXandroid: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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM