android 复选框大小,Android:如何更改CheckBox大小?

我找到了一种方法,无需创建自己的图像。 换句话说,系统图像正在缩放。 我不假装解决方案是完美的; 如果有人知道缩短某些步骤的方法,我会很高兴找到方法。

首先,我将以下内容放在项目的主要活动类(WonActivity)中。 这是直接从Stack Overflow获取的 - 谢谢你们!

/** get the default drawable for the check box */

Drawable getDefaultCheckBoxDrawable()

{

int resID = 0;

if (Build.VERSION.SDK_INT <= 10)

{

// pre-Honeycomb has a different way of setting the CheckBox button drawable

resID = Resources.getSystem().getIdentifier("btn_check", "drawable", "android");

}

else

{

// starting with Honeycomb, retrieve the theme-based indicator as CheckBox button drawable

TypedValue value = new TypedValue();

getApplicationContext().getTheme().resolveAttribute(android.R.attr.listChoiceIndicatorMultiple, value, true);

resID = value.resourceId;

}

return getResources().getDrawable(resID);

}

其次,我创建了一个“扩展可绘制”的类。 请注意,它与标准ScaleDrawable完全不同。

import android.graphics.drawable.*;

/** The drawable that scales the contained drawable */

public class ScalingDrawable extends LayerDrawable

{

/** X scale */

float scaleX;

/** Y scale */

float scaleY;

ScalingDrawable(Drawable d, float scaleX, float scaleY)

{

super(new Drawable[] { d });

setScale(scaleX, scaleY);

}

ScalingDrawable(Drawable d, float scale)

{

this(d, scale, scale);

}

/** set the scales */

void setScale(float scaleX, float scaleY)

{

this.scaleX = scaleX;

this.scaleY = scaleY;

}

/** set the scale -- proportional scaling */

void setScale(float scale)

{

setScale(scale, scale);

}

// The following is what I wrote this for!

@Override

public int getIntrinsicWidth()

{

return (int)(super.getIntrinsicWidth() * scaleX);

}

@Override

public int getIntrinsicHeight()

{

return (int)(super.getIntrinsicHeight() * scaleY);

}

}

最后,我定义了一个复选框类。

import android.graphics.*;

import android.graphics.drawable.Drawable;

import android.widget.*;

/** A check box that resizes itself */

public class WonCheckBox extends CheckBox

{

/** the check image */

private ScalingDrawable checkImg;

/** original height of the check-box image */

private int origHeight;

/** original padding-left */

private int origPadLeft;

/** height set by the user directly */

private float height;

WonCheckBox()

{

super(WonActivity.W.getApplicationContext());

setBackgroundColor(Color.TRANSPARENT);

// get the original drawable and get its height

Drawable origImg = WonActivity.W.getDefaultCheckBoxDrawable();

origHeight = height = origImg.getIntrinsicHeight();

origPadLeft = getPaddingLeft();

// I tried origImg.mutate(), but that fails on Android 2.1 (NullPointerException)

checkImg = new ScalingDrawable(origImg, 1);

setButtonDrawable(checkImg);

}

/** set checkbox height in pixels directly */

public void setHeight(int height)

{

this.height = height;

float scale = (float)height / origHeight;

checkImg.setScale(scale);

// Make sure the text is not overlapping with the image.

// This is unnecessary on Android 4.2.2, but very important on previous versions.

setPadding((int)(scale * origPadLeft), 0, 0, 0);

// call the checkbox's internal setHeight()

// (may be unnecessary in your case)

super.setHeight(height);

}

}

而已。 如果在视图中放置WonCheckBox并应用setHeight(),则复选框图像的大小合适。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值