我怎样才能使可绘制上的按钮变小?图标太大,其实比按钮高。这是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
可绘制绘制= 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
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
上一篇:isValidFragment Android的API 19Android、isValidFragment、API
下一篇:安卓4.4.2 - java.lang.RuntimeException的:活动未恢复执行停止java、RuntimeException、lang