[英]In an Android XML layout, can I use a child view or viewgroup to tint part of the parent's background?
I currently have the following layout as part of my XML file:我目前有以下布局作为我的 XML 文件的一部分:
LinearLayout (orientation: vertical)
+---TextView (title)
+---LinearLayout (orientation: horizontal)
+---ConstraintLayout
+---...
+---ConstraintLayout
+---...
+---ConstraintLayout
+---...
The parent LinearLayout
has a background that is a drawable.父
LinearLayout
有一个可绘制的背景。
My intention is to have the child TextView
and child LinearLayout
tint a part of said parent LinearLayout
's background each with different colors.我的目的是让子
TextView
和子LinearLayout
为所述父LinearLayout
的背景的一部分着色,每个背景都具有不同的颜色。
I've tried the backgroundTint
and foregroundTint
attributes in both the child layouts, but neither changed the parent LinearLayout
's drawable in any way.我已经在两个子布局中尝试了
backgroundTint
和foregroundTint
属性,但都没有以任何方式改变父LinearLayout
的可绘制对象。
So, is there any way I can do what I asked in the title, or am I stuck subclassing the LinearLayout
and overriding the onDraw
function?那么,有什么办法可以按照我在标题中的要求进行操作,还是我坚持
LinearLayout
并覆盖onDraw
函数?
Yup, I'm stuck with using the ViewGroup
's onDraw
function:是的,我坚持使用
ViewGroup
的onDraw
函数:
public class PartialTintedRoundedRectLinearLayout extends LinearLayout {
Context ctx;
RectF topPartitionRect, botPartitionRect;
RectF topBandAidRect, botBandAidRect;
Paint bgTopPaint, bgBotPaint;
int defaultCornerRadius;
float material30sdp;
public PartialTintedRoundedRectLinearLayout(Context c) {
super(c);
init(c);
}
public PartialTintedRoundedRectLinearLayout(Context c, AttributeSet a) {
super(c, a);
init(c);
}
public PartialTintedRoundedRectLinearLayout(Context c, AttributeSet a, int dsAttr) {
super(c, a, dsAttr);
init(c);
}
private void init(Context c) {
setWillNotDraw(false);
ctx = c;
defaultCornerRadius = (int)(20 * ctx.getResources().getDisplayMetrics().density);
material30sdp = ctx.getResources().getDimension(R.dimen._30sdp);
bgTopPaint = new Paint(); bgTopPaint.setColor(ContextCompat.getColor(ctx, R.color.blue_700));
bgBotPaint = new Paint(); bgBotPaint.setColor(ContextCompat.getColor(ctx, R.color.blue_500));
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
topPartitionRect = new RectF(0, 0, getWidth(), material30sdp);
topBandAidRect = new RectF(0, defaultCornerRadius, getWidth(), material30sdp);
botPartitionRect = new RectF(0, material30sdp, getWidth(), getHeight());
botBandAidRect = new RectF(0, material30sdp, getWidth(), getHeight() - defaultCornerRadius);
invalidate();
getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRoundRect(topPartitionRect, defaultCornerRadius, defaultCornerRadius, bgTopPaint);
canvas.drawRoundRect(botPartitionRect, defaultCornerRadius, defaultCornerRadius, bgBotPaint);
canvas.drawRect(topBandAidRect, bgTopPaint);
canvas.drawRect(botBandAidRect, bgBotPaint);
}
}
I'll have to figure out how to make it more general, since this was made for one specific use-case.我必须弄清楚如何使它更通用,因为这是为一个特定用例而设计的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.