【发布时间】:2018-09-25 08:07:57
【问题描述】:
我试图更好地理解图层可绘制对象在按钮可绘制对象中的工作方式。
我正在尝试绘制 2 个简单的彩色框,其中一个没有插图,以便填充整个按钮可绘制区域。还有一个带有一些插图的。
ColorDrawable background1 = new ColorDrawable(Color.BLUE);
ColorDrawable background2 = new ColorDrawable(Color.GREEN);
Drawable[] drawables = new Drawable[] {
background1,
background2
};
LayerDrawable ld = new LayerDrawable(drawables);
ld.setLayerInset(0, 0, 0, 0, 0 ); // no inset on white box
ld.setLayerInset(1, 8, 8, 8, 8 ); // 8 inset on all side on green box
// set the left drawable on the button
button.setCompoundDrawablesWithIntrinsicBounds(ld, null, null, null);
但是这根本不起作用。第一个问题是盒子没有填满任何区域。那是因为按钮可绘制对象没有预定义的大小吗?如果是这种情况,我尝试在盒子上手动设置边界,但也没有太多运气。
谁能帮我理解我做错了什么?
【问题讨论】:
-
@pskink 谢谢。我不完全理解创建 BitmapDrawable 如何解决我的问题,您能进一步解释一下吗? IE 我将如何创建一个“填充”按钮 leftDrawable 空间的 bitmapdrawable?
-
不,我想在按钮可绘制区域中创建自己的“图像”。 IE 按钮有一个 leftDrawable、rightDrawable 等。我正在尝试以编程方式进行操作。所以以编程方式进行此操作,我试图通过在上面绘制 2 个彩色框来开始简单。只是想弄清楚它是如何工作的。
标签: android android-drawable android-button layerdrawable