Android Layer_可作为按钮绘制
我有一个钮扣。 该按钮由StateListDrawable(由3个9-patch图像组成)构建。 我需要添加一个额外的绘图,将驻留在按钮的右侧,我需要它与按钮的右侧对齐。 我尝试使用以下方法:Android Layer_可作为按钮绘制,android,image,alignment,drawable,Android,Image,Alignment,Drawable,我有一个钮扣。 该按钮由StateListDrawable(由3个9-patch图像组成)构建。 我需要添加一个额外的绘图,将驻留在按钮的右侧,我需要它与按钮的右侧对齐。 我尝试使用以下方法: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable=
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/blue_back_button_drawable" />
<item>
<bitmap android:src="@drawable/filter_by_button_v_mark" android:gravity="right"/>
</item>
</layer-list>
它不起作用,而且V图像确实是右对齐的,但它与按钮右侧有一个差异。下面是一个快照:
我希望图像与按钮的左侧对齐,目前我认为我能做到的唯一方法是:
我不应该提及这听起来很可怕:-)我希望有更好的办法。哦,它不是图像的一部分的原因是,我需要知道它的宽度,以便我可以计算文本填充,这样它就不会被按钮的文本隐藏,因为它不像9面片那么好看。似乎没有合适的解决方案。图层显然是静态的,而不是9面片图像,或者看起来是这样。 所以我扩展了按钮,改变了它的绘制状态,我必须使用Canvas.restore()来打破边缘,将我的边缘向下推,并将我的图像放在右上角。。。 代码如下:
@Override
public void setPadding(int left, int top, int right, int bottom) {
right += mImageWidth;
super.setPadding(left, top, right, bottom);
}
private void setVImageDrawable(Drawable d) {
mVImageDrawable = d;
mImageWidth = mVImageDrawable.getIntrinsicWidth();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if( mVImageDrawable != null && mIsMarked){
//for some reason the clip binds us to position with margins, which is not so good for us.
//we need to save the clip, restore the canvas to pre clipping mode, do what we need then
//put the canvas back to it's previous clipping state.
Rect bounds = canvas.getClipBounds();
canvas.restore();
int right = getRight();
int left = right - mImageWidth;
int top = getTop();
int bottom = top + mVImageDrawable.getIntrinsicHeight();
mVImageDrawable.setBounds(left, top, right, bottom);
mVImageDrawable.draw(canvas);
canvas.save();
canvas.clipRect(bounds);
}
}
如果我没有按添加的任何边距将原始画布iget“向下推”,它也可能会被修复,我认为使用xml属性指定是否应在包含此按钮的布局上剪切子项,但我发现这是一个蹩脚的解决方案,因为它使我的按钮依赖于我们的条件。
我使用我声明的新属性、样式和主题从XML中获取的图像本身
希望它能帮助一些人。似乎没有合适的解决方案。图层显然是静态的,而不是9面片图像,或者看起来是这样。 所以我扩展了按钮,改变了它的绘制状态,我必须使用Canvas.restore()来打破边缘,将我的边缘向下推,并将我的图像放在右上角。。。 代码如下:
@Override
public void setPadding(int left, int top, int right, int bottom) {
right += mImageWidth;
super.setPadding(left, top, right, bottom);
}
private void setVImageDrawable(Drawable d) {
mVImageDrawable = d;
mImageWidth = mVImageDrawable.getIntrinsicWidth();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if( mVImageDrawable != null && mIsMarked){
//for some reason the clip binds us to position with margins, which is not so good for us.
//we need to save the clip, restore the canvas to pre clipping mode, do what we need then
//put the canvas back to it's previous clipping state.
Rect bounds = canvas.getClipBounds();
canvas.restore();
int right = getRight();
int left = right - mImageWidth;
int top = getTop();
int bottom = top + mVImageDrawable.getIntrinsicHeight();
mVImageDrawable.setBounds(left, top, right, bottom);
mVImageDrawable.draw(canvas);
canvas.save();
canvas.clipRect(bounds);
}
}
如果我没有按添加的任何边距将原始画布iget“向下推”,它也可能会被修复,我认为使用xml属性指定是否应在包含此按钮的布局上剪切子项,但我发现这是一个蹩脚的解决方案,因为它使我的按钮依赖于我们的条件。
我使用我声明的新属性、样式和主题从XML中获取的图像本身
希望它能帮助一些ppl。快照因某些原因无法显示:-(快照因某些原因无法显示:-)(你能详细告诉我你需要什么吗?你能详细告诉我你需要什么吗?