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

Ard*_*ebi 50 android

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

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

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="tr.k12.evrim.evrimnews.MainActivity"
    tools:showIn="@layout/activity_main">




    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/frameLayout"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Giri? Yap"
                android:onClick="SignIn"
                android:textStyle="bold"
                style="@style/Widget.AppCompat.Button.Colored"/>




            </LinearLayout>


    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_below="@id/frameLayout"
        android:orientation="vertical">



    <Button
        android:text="Duyurular"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_below="@+id/frameLayout"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="13dp"
        android:id="@+id/button2"
        android:drawableLeft="@drawable/megaphone" />

    <Button
        android:text="Kadromuz"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_below="@+id/button2"
        android:layout_alignEnd="@+id/button2"
        android:layout_marginTop="13dp"
        android:id="@+id/button3"
        android:drawableLeft="@drawable/puzzle"/>

        <Button
            android:text="Ba?ar?lar?m?z"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginTop="12dp"
            android:id="@+id/button5"
            android:drawableLeft="@drawable/trophy"/>


    <Button
        android:text="Ortaklar?m?z"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginTop="12dp"
        android:id="@+id/button4"
        android:drawableLeft="@drawable/deal"/>




    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Dro*_*der 143

将您的资源包装在一个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 >
Run Code Online (Sandbox Code Playgroud)

之后,在android:drawableLeft标签中使用此drawable

  • @MuhammedRefaat小心这个解决方案,属性*width*和*height*仅与API 23或更新版本兼容,因此它不适用于Lollipop和版本. (72认同)
  • 作为初学者,我不明白到底该怎么做。请添加更多详细信息。 (2认同)

Ras*_*iri 21

更新:

只需使用具有 icon 属性的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"/>
Run Code Online (Sandbox Code Playgroud)


小智 15

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

app:icon
app:iconGravity
app:iconPadding
app:iconSize
Run Code Online (Sandbox Code Playgroud)


小智 7

您可以通过编程方式(在此处)设置范围,如下所示

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


Gib*_*olt 7

使用 Kotlin 扩展:

添加它以保持您的代码清洁和可重用。如果您不为 id 传递任何内容或 0,它将清除可绘制对象。(按钮扩展 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)
}
Run Code Online (Sandbox Code Playgroud)


see*_*ess 6

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

在此输入图像描述

这就是API 23+的外观

在此输入图像描述

它仍然是一个非常好的选择,即使最小API <23.


rev*_*pod 5

尝试以下方法。将可绘制对象声明为要使用的图标

    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);
Run Code Online (Sandbox Code Playgroud)


Ozz*_*ant 5

只要图标是一个矢量资源,走在矢量文件中,并修改android:widthandroid:height喜欢的东西@dimen/icon_size。然后在您的 dimens.xml 文件中为多种屏幕尺寸定义此资源。奇迹般有效