简体   繁体   English

在 Android XML 布局中,我可以使用子视图或视图组为父视图的一部分背景着色吗?

[英]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.我已经在两个子布局中尝试了backgroundTintforegroundTint属性,但都没有以任何方式改变父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:是的,我坚持使用ViewGrouponDraw函数:

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.

相关问题 在Android布局中无法将视图转换为ViewGroup - View can not be cast in to ViewGroup in android layout 如何在android xml visualizer的父垂直线性布局中包含子水平线性布局? - How can i include a child horizontal linear layout in a parent vertical linear layout in android xml visualizer? 如何将单击事件从父(ViewGroup)委托给子(View)android? - How to delegate click event from parent (ViewGroup) to child (View) android? Android:父视图组ontouch被忽略,有利于子视图onclick - Android: parent Viewgroup ontouch being ignored in favour of child view onclick 如何让孩子的边距影响Android中父母的视图大小? - How can I have a child's margins affect the parent's view size in Android? 在Android中将子视图与父布局的边缘对齐 - Align child view to edge of parent layout in Android android:tint在sdk &lt;21上的布局的背景可绘制对象上 - android:tint on background drawable of a layout on sdk<21 无法将子视图插入Android Studio的布局编辑器 - Can not insert child view into Android Studio's layout editor 在Android中实现自定义View和/或ViewGroup(流布局) - Implementing custom View and/or ViewGroup (Flow Layout) in Android 如何在父视图中使用OnClickListener,在子视图中使用onTouchEvent? - How can I use OnClickListener in parent View and onTouchEvent in Child View?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM