我怎样才能缩小按钮上的绘制? [英] How can I shrink the drawable on a button?

查看:176
本文介绍了我怎样才能缩小按钮上的绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能使可绘制上的按钮变小?图标太大,其实比按钮高。这是code我使用的:

 <按钮
    机器人:背景=@可绘制/ red_button
    机器人:drawableLeft =@可绘制/ s_vit
    机器人:ID =@ + ID / ButtonTest
    机器人:重力=左| center_vertical
    机器人:文本=S系列计算器
    机器人:文字颜色=@机器人:彩色/白
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_marginLeft =25dp
    机器人:layout_marginRight =25dp
    机器人:drawablePadding =10dp>
    < /按钮>
 

上是应该如何看,下它的外观现在。

我试过,但没有显示的图像。 : - (

 资源资源= getResources();
    ScaleDrawable SD =新ScaleDrawable(res.getDrawable(R.drawable.s_vit),0,10F,10F);
    按钮BTN =(按钮)findViewById(R.id.ButtonTest);
    btn.setCompoundDrawables(sd.getDrawable(),NULL,NULL,NULL);
 

解决方案

您应该使用的ImageButton ,并指定 Android上的图像:SRC ,并设定安卓scaletype fitXY


设置比例绘制在code

 可绘制绘制= getResources()getDrawable(R.drawable.s_vit)。
drawable.setBounds(0,0,(int)的(drawable.getIntrinsicWidth()* 0.5),
                         (中间体)(drawable.getIntrinsicHeight()* 0.5));
ScaleDrawable SD =新ScaleDrawable(绘制,0,scaleWidth,scaleHeight);
按钮BTN = findViewbyId(R.id.yourbtnID);
btn.setCompoundDrawables(sd.getDrawable(),NULL,NULL,NULL); //设置drawableLeft例如
 

how can I make the drawable on a button smaller? The icon is too big, actually higher than the button. This is the code I am using:

    <Button
    android:background="@drawable/red_button"
    android:drawableLeft="@drawable/s_vit"
    android:id="@+id/ButtonTest"
    android:gravity="left|center_vertical" 
    android:text="S-SERIES CALCULATOR"
    android:textColor="@android:color/white"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    android:drawablePadding="10dp">
    </Button>

The upper is how it should look, the lower how it looks right now.

I tried this but there is no image displayed. :-(

    Resources res = getResources();
    ScaleDrawable sd = new ScaleDrawable(res.getDrawable(R.drawable.s_vit), 0, 10f, 10f);
    Button btn = (Button) findViewById(R.id.ButtonTest);
    btn.setCompoundDrawables(sd.getDrawable(), null, null, null);

解决方案

You should use a ImageButton and specify the image in android:src, and set android:scaletype to fitXY


Setting scaled drawable in code

Drawable drawable = getResources().getDrawable(R.drawable.s_vit);
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); //set drawableLeft for example

这篇关于我怎样才能缩小按钮上的绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆