This question already has answers here:
how to change a drawableLeft icon size on a button?
                                
                                    (5个答案)
                                
                        
                                去年关闭。
            
                    
我正在寻找一些解决方案how to change a drawableLeft icon size on a button?,但对我来说不起作用。

我的按钮布局:

<RelativeLayout
        android:id="@+id/twoButtonLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#eceef5"
        android:visibility="visible">

        <Button
            android:id="@+id/goToPersonalPage"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_marginLeft="31dp"
            android:drawableLeft="@drawable/drawable_left_green"
            android:paddingLeft="18dp"
            android:drawablePadding="-10dp"
            android:background="@drawable/button_oval"
            android:text="ICon"
            android:textColor="#29395e"
            android:textSize="18dp" />

        <Button
            android:id="@+id/logOutPersonal"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="30dp"
            android:background="@drawable/button_oval"
            android:text="@string/logOut"
            android:textColor="#29395e"
            android:textSize="18dp" />

    </RelativeLayout>


我通过drawable_left_green.xml调整按钮的大小:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    // I think it should be more smaller if i set 15sp.
    <item
        android:drawable="@mipmap/ic_launcher"
        android:width="15sp"
        android:height="15sp"
        />

</layer-list >


当我编译项目时,它(photo2)看起来不像预览布局(photo1):
android - 如何更改drawableLeft图标的大小?-LMLPHP
android - 如何更改drawableLeft图标的大小?-LMLPHP

我不知道为什么会这样发生,有人可以教我如何解决它?
任何帮助将不胜感激,在此先感谢。

最佳答案

您从我的链接中获得了答案,但我想给您答案,这样对您有所帮助。

在代码中设置缩放的可绘制对象

 Drawable drawable = ContextCompat.getDrawable(YourActivity.this,R.drawable.akshay);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.5),
                     (int)(drawable.getIntrinsicHeight()*0.5));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, scaleWidth, scaleHeight);
Button btn = findViewbyId(R.id.yourbtnID);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null);

关于android - 如何更改drawableLeft图标的大小? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44583839/

10-10 16:06