【问题标题】:Floating action button (Fab) Icon size problems after migrating to sdk 28浮动操作按钮 (Fab) 迁移到 sdk 28 后的图标大小问题
【发布时间】:2019-07-09 04:22:17
【问题描述】:

因此,在迁移到 SDK 28 后,图标尺寸变得太小并且向左对齐,这很奇怪,一切都在工作尝试设置一个空项目,只是使用了一个带有图标的浮动操作按钮,在调整图标大小后变得很奇怪,就像这样在下面提供的图片中。

这是它的外观图片:

尝试将FAB中的缩放类型属性设置为center,但不起作用,也将其设置为fitcenter,它也不起作用。

这是我的浮动操作按钮代码:

   <com.google.android.material.floatingactionbutton.FloatingActionButton
       android:id="@+id/fab2"
       android:padding="0dp"
       android:scaleType="fitCenter"
       android:layout_gravity="center"
       android:layout_width="@dimen/_63sdp"
       android:layout_height="@dimen/_63sdp"
       android:layout_marginStart="8dp"
       android:layout_marginTop="8dp"
       app:fabSize="auto"
       android:clickable="true"
       android:focusable="true"
       app:srcCompat="@drawable/ic_brush"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent" />

画笔图标矢量XML代码:


<vector android:height="24dp" android:tint="#F8F7FF"
    android:viewportHeight="24.0" android:viewportWidth="24.0"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FF000000" android:pathData="M7,14c-1.66,0 -3,1.34 -3,3 0,1.31 -1.16,2 -2,2 0.92,1.22 2.49,2 4,2 2.21,0 4,-1.79 4,-4 0,-1.66 -1.34,-3 -3,-3zM20.71,4.63l-1.34,-1.34c-0.39,-0.39 -1.02,-0.39 -1.41,0L9,12.25 11.75,15l8.96,-8.96c0.39,-0.39 0.39,-1.02 0,-1.41z"/>
</vector>

Dimen 是我用来调整屏幕尺寸的库,但别担心,我也试过不使用它,但问题仍然存在。

它只有在我添加后才能正常工作(但没有它也应该可以工作):

fab.setScaleType(ImageView.ScaleType.FIT_CENTER);
fab2.setScaleType(ImageView.ScaleType.FIT_CENTER);

【问题讨论】:

  • 试试 app:fabSize="mini"
  • 试过了,还是不行。

标签: android android-layout


【解决方案1】:

我认为自定义 android:layout_width="@dimen/_63sdp"android:layout_height="@dimen/_63sdp" 的问题

尝试将android:layout_width="wrap_content"android:layout_height="wrap_content"app:fabCustomSize="@dimen/_63sdp" 一起使用

示例代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    android:orientation="vertical">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab2"
        android:layout_width="@dimen/_63sdp"
        android:layout_height="@dimen/_63sdp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:clickable="true"
        android:focusable="true"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_arrow_right_white"
        app:fabCustomSize="30dp"
        app:fabSize="auto"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:maxImageSize="@dimen/_30sdp" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/ic_delete_white"
        app:backgroundTint="@android:color/holo_red_dark"
        app:fabCustomSize="@dimen/_63sdp"
        app:maxImageSize="50dp" />

</LinearLayout>

输出

【讨论】:

  • 好吧,我相信它会起作用,但它会导致按钮尺寸减小到变得太小,而不是获得屏幕所需的按钮尺寸。
  • @AmirEmad 您可以使用app:fabCustomSize="@dimen/_63sdp" 设置您所需的工厂大小,也可以使用app:maxImageSize="50dp" 更改图标大小
  • 现在测试一下。
  • @AmirEmad 好的,如果有任何问题,请告诉我
  • 完美运行。唯一的问题是我必须为每个布局设置不同的最大尺寸,因为图标会过大以适合或过小。我仍然想知道为什么 Google 会以旧方式更改它,即之前会自动处理图标大小。不管怎样,我用app:maxImageSize="@dimen/_30sdp" 试了一下,看起来好多了。非常感谢,但我仍然希望找到一个可以自行处理图标大小的解决方案。
【解决方案2】:

所以如果有人遇到同样的问题:

到目前为止,还没有足够好的 XML 选项来解决这个问题(如果你 知道的请发布问题的答案,将不胜感激)

但是你可以通过编程来做到这一点:

 FloatingActionButton fab = findViewById(R.id.fab);
 fab.setScaleType(ImageView.ScaleType.FIT_CENTER);

【讨论】:

    【解决方案3】:

    使用 app:fabCustomSize="@dimen/_63sdp" ,如果无效则删除 app:fabSize="auto"

    【讨论】:

    • 试过了,它工作并将图标放在中间,但它仍然小于所需的按钮大小。
    【解决方案4】:

    简单使用:

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_img" />
    

    使用这个依赖(暂时使用这个):

    implementation 'com.google.android.material:material:1.0.0'
    

    Alpha 版本库:

    implementation 'com.google.android.material:material:1.1.0-alpha06'
    

    Further reading

    【讨论】:

    • 我已经在使用非 alpha 库版本。你的意思是我应该改用 alpha 版本吗?
    • 不,不要使用它。
    • 另外,环绕内容会导致按钮尺寸过小。
    【解决方案5】:

    像这样设置 dimension 属性以避免这个问题:

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     app:fabSize="normal"
    

    【讨论】:

      【解决方案6】:

      使用 maxImageSize 属性:

      <com.google.android.material.floatingactionbutton.FloatingActionButton
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="end|bottom"
              app:fabCustomSize="100dp"
              app:maxImageSize="60dp"
              app:srcCompat="@drawable/ic_employer" />
      

      【讨论】:

        猜你喜欢
        • 2019-02-09
        • 2023-03-15
        • 2018-03-04
        • 1970-01-01
        • 2016-01-13
        • 2016-07-02
        • 1970-01-01
        相关资源
        最近更新 更多