【问题标题】:How to force Android XML vector graphics to scale to fill an image button?如何强制Android XML矢量图形缩放以填充图像按钮?
【发布时间】:2021-09-09 00:16:27
【问题描述】:

我为我的应用在屏幕底部制作了自己的操作栏版本,但遇到了一个令人费解的问题,即放置在该操作栏按钮中的图标。布局真的很简单。

    <LinearLayout
        android:id="@+id/navbar"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:layout_alignParentBottom="true"
        android:elevation="1dp"
        android:gravity="center"
        android:background="@color/black_overlay" >

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|center"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:backgroundTintMode="multiply"
            android:backgroundTint="@android:color/transparent"
            android:foregroundTint="@color/white"
            android:src="@drawable/ic_list_48"
            android:contentDescription="Map Button" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center|center"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:backgroundTintMode="multiply"
            android:backgroundTint="@android:color/transparent"
            android:src="@android:drawable/ic_popup_reminder"
            android:contentDescription="Notifications Button" />

我遇到的问题是,当使用上面“通知按钮”中的“系统”图标时,该图标会正确缩放以填充 ImageButton。但是,当使用从系统图标(如上面的“地图按钮”)创建的图标时,图标不会像应有的那样缩放以填充 ImageButton。有没有办法让它这样做?

【问题讨论】:

  • 您是否尝试过使用android:scaleType="fitxy",如果@drawable/ic_list_48 是方形的,它应该可以正常工作。
  • 我在 ImageButton 上试过了,但什么也没做。
  • @drawable/ic_list_48 用这个作为背景

标签: android android-studio android-layout android-vectordrawable


【解决方案1】:

试试

android:scaleType="fitxy"

【讨论】:

  • 谢谢,我试过了,但没有任何效果。
  • 实际上,除了@Cheticamp 的上述答案之外,这是必要的。
【解决方案2】:

您的图标不会按比例放大以适应 ImageButtons,而是 ImageButtons 会按比例放大以适应每个图标都具有固有宽度,因为您指定了 'wrap_content '。你可以在这里看到:

左侧按钮具有标准矢量可绘制启动图标(宽度/高度=108dp),右侧图标是系统定义的 PNG,大小因平台而异。

您没有指定布局的外观,但我建议如下:

<LinearLayout 
    android:id="@+id/navbar"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:layout_alignParentBottom="true"
    android:background="@color/black"
    android:elevation="1dp"
    android:gravity="center"
    tools:context=".MainActivity">

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center|center"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_weight="1"
        android:backgroundTint="@android:color/transparent"
        android:backgroundTintMode="multiply"
        android:contentDescription="Map Button"
        android:foregroundTint="@color/white"
        android:src="@drawable/ic_launcher_foreground" />

    <ImageButton
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_weight="1"
        android:backgroundTint="@android:color/transparent"
        android:backgroundTintMode="multiply"
        android:contentDescription="Notifications Button"
        android:src="@android:drawable/ic_popup_reminder" />
</LinearLayout>  
             

这里使用权重来扩大图标的大小。结果如下:

【讨论】:

  • 对我有用的解决方案是@Aashutosh 下面的答案和这个答案的一部分。特别是从这个答案中,将 WIDTH 设置为零并将 layout_weight 设置为 1 是造成差异的原因。不过,我不确定为什么这能代替 WRAP_CONTENT 起作用。
  • 另外,按照我的做法,Android Studio 中的布局视图始终显示按钮大小相同。只有 Drawable(图标)会改变大小。您可以看到这一点,因为拆分视图中的两个图像在一个面板上显示轮廓,而在另一个面板上显示内容。虽然请注意我使用“fitCenter”而不是“fitXY”以获得更清晰的外观。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 2020-05-12
  • 2014-10-03
  • 1970-01-01
相关资源
最近更新 更多