如何在按钮上更改drawableLeft图标大小?

时间:2016-12-06 15:25:12

标签: android

我在4个按钮上添加了一些图标,它们在按钮旁边看起来很大。那么我该如何调整它们的大小呢?我在按钮上使用了drawableLeft来添加图标。

drawables被称为交易,奖杯,拼图和扩音器。图标太大了。 content_main.xml文件:

import numpy as np
import matplotlib.pyplot as plt
In [13]:

# Data
Lat = np.array([-65.62282562, -65.62266541, -65.62241364, -65.62398529, -65.62410736])
Lon = np.array([145.28251648, 145.38883972, 145.49528503, 121.4509201, 121.55738068, 121.66372681])
C = np.array([0., 0.5, 2, 3, 0])


# Plot
plt.pcolormesh(X, Y, C)

10 个答案:

答案 0 :(得分:126)

将您的资源包装在一个drawable中,该drawable定义了您所需的大小,类似于:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

  <item
      android:drawable="@drawable/icon"
      android:width="@dimen/icon_size"
      android:height="@dimen/icon_size"
      />

</layer-list >

之后,请在android:drawableLeft标记

中使用此drawable

答案 1 :(得分:6)

添加DroidBender的绝佳答案。他的解决方案在API 21上没有崩溃,因此即使不添加Build.VERSION.SDK_INT代码,它仍然可以使用。这就是使用标准图像资源的解决方案在API 21上的外观(使用文本高度作为维度设置)。

enter image description here

这就是它在API 23 +

上的表现

enter image description here

它仍然是一个非常好的选择,即使最小的API&lt; 23。

答案 2 :(得分:5)

使用Kotlin扩展程序:

添加此代码可保持代码干净和可重复使用。如果您未传递任何内容或将0作为id传递,它将清除可绘制对象。 (按钮扩展了TextView)

buttonView.leftDrawable(R.drawable.my_icon, R.dimen.icon_size)

// Button extends TextView
fun TextView.leftDrawable(@DrawableRes id: Int = 0, @DimenRes sizeRes: Int) {
    val drawable = ContextCompat.getDrawable(context, id)
    val size = resources.getDimensionPixelSize(sizeRes)
    drawable?.setBounds(0, 0, size, size)
    this.setCompoundDrawables(drawable, null, null, null)
}

答案 3 :(得分:5)

只需使用Google的MaterialButtons: https://material.io/develop/android/components/buttons/

app:icon
app:iconGravity
app:iconPadding
app:iconSize

答案 4 :(得分:4)

您可以以编程方式设置边界(here),这看起来像这样

Drawable img = ActivityName.this.getContext().getResources().getDrawable(
            R.drawable.blue_line);
img.setBounds(left, top, right, bottom);
button.setCompoundDrawables(img, null, null, null);

答案 5 :(得分:3)

尝试以下方法。将drawables声明为您要使用的图标

    Drawable drawable = getResources().getDrawable(R.mipmap.youricon);
    drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * 0.6),
            (int) (drawable.getIntrinsicHeight() * 0.6));
    ScaleDrawable sd = new ScaleDrawable(drawable, 0, 40, 40);
    youredittext1.setCompoundDrawables(null, null, sd.getDrawable(), null);
    drawable = getResources().getDrawable(R.mipmap.yourothericon);
    drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * 0.6),
            (int) (drawable.getIntrinsicHeight() * 0.6));
    sd = new ScaleDrawable(drawable, 0, 40, 40);
    youredittext2.setCompoundDrawables(null, null, sd.getDrawable(), null);

答案 6 :(得分:2)

只要图标是矢量资产,就可以进入矢量文件并将android:widthandroid:height修改为@dimen/icon_size之类的内容。然后在您的dimens.xml文件中为多个屏幕尺寸定义此资源。像魅力一样工作

答案 7 :(得分:2)

更新:

只需使用MaterialButton 它有图标属性,例如:

app:icon
app:iconSize
app:iconGravity
app:iconPadding



<com.google.android.material.button.MaterialButton
    app:icon="@drawable/test"
    app:iconSize="8dp"
    app:iconGravity="end"
    app:iconPadding="8dp"/>

答案 8 :(得分:0)

该解决方案适用于使用矢量图形图标的所有使用情况,并且还与以下23个版本兼容。

在xml中打开可绘制图标

可绘制的原始图标:

Result: Parcel(


0x00000000: ffffffff 0000005e 006f0043 00790070 '....^...C.o.p.y.'
  0x00000010: 006e0069 00200067 006d0073 00200073 'i.n.g. .s.m.s. .'
  0x00000020: 006f0074 00550020 00630049 003a0063 't.o. .U.I.c.c.:.'
  0x00000030: 004e0020 00690065 00680074 00720065 ' .N.e.i.t.h.e.r.'
  0x00000040: 00750020 00650073 00200072 00300032 ' .u.s.e.r. .2.0.'
  0x00000050: 00300030 006e0020 0072006f 00630020 '0.0. .n.o.r. .c.'
  0x00000060: 00720075 00650072 0074006e 00700020 'u.r.r.e.n.t. .p.'
  0x00000070: 006f0072 00650063 00730073 00680020 'r.o.c.e.s.s. .h.'
  0x00000080: 00730061 00610020 0064006e 006f0072 'a.s. .a.n.d.r.o.'
  0x00000090: 00640069 0070002e 00720065 0069006d 'i.d...p.e.r.m.i.'
  0x000000a0: 00730073 006f0069 002e006e 00450052 's.s.i.o.n...R.E.'
  0x000000b0: 00450043 00560049 005f0045 004d0053 'C.E.I.V.E._.S.M.'
  0x000000c0: 002e0053 00000000                   'S.......        ')

现在,使用组标签包装路径标签,并在组标签中添加scaleX,scaleY,pivotX和ivotY属性,以缩小图标大小,如下所示:

<vector 
    android:height="24dp"
    android:tint="?attr/colorControlNormal" 
    android:viewportHeight="24"
    android:viewportWidth="24" 
    android:width="24dp" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M11,18h2v-2h-2v2zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8zM12,6c-2.21,0 -4,1.79 -4,4h2c0,-1.1 0.9,-2 2,-2s2,0.9 2,2c0,2 -3,1.75 -3,5h2c0,-2.25 3,-2.5 3,-5 0,-2.21 -1.79,-4 -4,-4z"/>
</vector>

您可以通过更改scaleX和scaleY来控制图标的大小。

这不仅适用于可绘制对象,而且适用于使用图标的任何地方,以及在不容易找到减小图标尺寸的样式解决方案的地方,例如AlertDialogue。

答案 9 :(得分:0)

使用 ImageView 的最佳方式。 这是一个管理文本视图左右两个图像的示例。

<ImageView
        android:id="@+id/manage_icon_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingStart="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:foregroundGravity="center"
        android:src="@drawable/manage_store_icon"
      />
    <TextView
        android:id="@+id/manage_store_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:textColor="@color/black"
        android:text="@string/manage_store"
        android:layout_toRightOf="@+id/manage_icon_image"
        android:textSize="15sp"/>
    <ImageView
        android:id="@+id/ManageStoresLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="5dp"
        android:padding="5dp"
        android:onClick="onManageStoresLink"
        android:src="@drawable/manage_arrow_icon"
        app:layout_constraintTop_toBottomOf="@+id/my_ic_cards"
        />

enter image description here