【问题标题】:Wrap button around icon?环绕图标的按钮?
【发布时间】:2020-12-24 16:17:23
【问题描述】:

如何消除图标后的间隙?我怎样才能正常环绕图标周围的按钮?

    <Button
        android:layout_marginStart="4dp"
        android:id="@+id/button"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:backgroundTint="@color/white"

        
        app:icon="@drawable/googleg_standard_color_18"
        app:iconTint="@null"/>

【问题讨论】:

  • 您正在使用Button。这是为带有可选可绘制对象的文本标题而设计的。如果您只想要图标,请使用ImageButton
  • @CommonsWare 如果我用 imagebutton 替换,按钮会折叠成一个小矩形。看这里:i.imgur.com/qe4Egl9.png
  • 这就是我不使用图片按钮的原因
  • 您可以使用android:src,而不是app:icon。见the documentation
  • @CommonsWare 好的,现在尺寸很好,但我的自定义图标显示没有颜色。为什么?

标签: android xml android-button material-components-android


【解决方案1】:
  • 您可以按照 cmets 中的建议使用 ImageButton

  • 您可以将 MaterialButton 与这种样式一起使用:

    <style name="Widget.MaterialComponents.Button.IconOnly">
      <item name="iconPadding">0dp</item>
      <item name="android:insetTop">0dp</item>
      <item name="android:insetBottom">0dp</item>
      <item name="android:paddingLeft">12dp</item>
      <item name="android:paddingRight">12dp</item>
      <item name="android:minWidth">48dp</item>
      <item name="android:minHeight">48dp</item>
      <item name="iconGravity">textStart</item>
    </style>
    

类似:

  <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/Widget.MaterialComponents.Button.IconOnly"
            app:icon="@drawable/...."
            app:iconTint="@null"/>

【讨论】:

    【解决方案2】:

    我没有看到任何直接的方法,但做了一些调整来帮助你

    根据您的设计外观和感觉创建不带包装内容的按钮。

     <Button
        android:id="@+id/button"
        android:layout_width="42dp"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_google_hangouts"
        app:backgroundTint="@android:color/white" />
    

    在代码中进行以下更改,并根据您的要求调整填充。

     val button = findViewById<Button>(R.id.button)
        button.setPadding(24,0,0,0)
    

    我用来测试的图片尺寸是24dpX24dp

    我的最终结果:

    【讨论】:

      猜你喜欢
      • 2017-07-14
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多