【问题标题】:android put button at edge of viewandroid将按钮放在视图边缘
【发布时间】:2021-09-01 11:16:27
【问题描述】:

我正在尝试在 image view 的角上添加 floating action button。我查看了 stackoverflow 上所有当前的解决方案,但我遇到的答案并不详细,只包含诸如“使用布局锚点”或“使用框架布局”之类的广泛信息。我该怎么做呢?顺便说一句,我使用的根布局是constraint layout

【问题讨论】:

    标签: android xml android-constraintlayout floating-action-button


    【解决方案1】:

    您说您正在使用约束布局,因此您可以将按钮设置在该灰色背景视图的右下角。

    在视图的 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"/>
    
    ...
    
    

    【讨论】:

      【解决方案2】:

      你可以使用属性: 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>
      

      它看起来像:

      【讨论】:

      • wrap content 用于coordinator layout 属性时,我将如何获得相同的效果?目前,使用wrap content 使按钮进入imagview。我想将我的基本布局保留为constraint layout,并仅为此imageviewbutton 嵌套coordinator layout
      • 作为一种解决方法,您可以将margin 属性添加到ImageView&lt;ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:srcCompat="@tools:sample/avatars" android:layout_marginEnd="32dp" android:layout_marginBottom="32dp"/&gt;
      【解决方案3】:

      输出: Code Output

      布局文件:

      <?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>
      

      【讨论】:

        【解决方案4】:

        您可以将两者都包装在 ConstraintLayout 中,并操纵对晶圆厂顶部和底部的约束。

        但这还不足以让按钮参与图像,因此,您可以通过使用 android:scaleXandroid:scaleY 缩放 FAB 和/或图像并将 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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-24
          相关资源
          最近更新 更多