Extending GridLayout
Asked Answered
T

1

9

With GridLayout this is a valid layout definition. There's no warning about 'layout_height' attribute should be defined or 'layout_width' attribute should be defined

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView />
</GridLayout>

On the otherhand, if I extend GridLayout the equivalent layout gives both warnings 'layout_height' attribute should be defined and 'layout_width' attribute should be defined

<ExtendedGridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView />
</ExtendedGridLayout>

this is what the extended gridlayout looks like

package com.github.extendedgridlayout;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.GridLayout;

@SuppressWarnings("unused")
public class ExtendedGridLayout extends GridLayout {
    public ExtendedGridLayout(Context context){
        super(context);
    }

    public ExtendedGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExtendedGridLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ExtendedGridLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}

I tried looking through the GridLayout source, and it seems like what they did was to extend ViewGroup.LayoutParams and set a default width and height, just like for PercentRelativeLayout

So it should seem that based off of inheritance, ExtendedGridLayout should also set a default width and height for it's children or do whatever it is that GridLayout does to avoid the warning message in the layout editor.

So my question is why does ExtendedGridLayout have the warning and how do I prevent it?

Tragus answered 13/7, 2016 at 2:21 Comment(7)
I personally prefer creating my own view with LinearLayouts instead of using useless GridLayout.Verdun
What is the <ImageView /> for? Why wouldn't you want to give it width and height?Accouter
@uval gridview sets the width and height of it's children. Especially when you explicitly state the colum and row countTragus
I don't understand why you added an empty ImageView inside the xml. Is it pseudo-code? Is it your real code? Or is it accidental? (I've never seen such a use of a completely empty view in xml)Accouter
@uval it's a stripped down version to show the problem I'm havingTragus
So a <ImageView ... /> would be clearer. Because <ImageView /> can actually compile without an error, even if it has no meaning. So it could be a problem.Accouter
@uval it's meant to be able to compileTragus
P
2

That is the default behavior of AndroidStudio.
One way to avoid that error is suppressing.

<ExtendedGridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!--suppress AndroidDomInspection -->
    <ImageView />

</ExtendedGridLayout>

AndroidStudio skips showing that error with GridLayout, But does not skip with the children of GridLayout. Here is the source code of inspector of AndroidStudio.

Here is related bug report.
By this bug report, your issue occurred like this.

Promiscuous answered 19/7, 2016 at 1:52 Comment(2)
The bug report is different from my situation. In the bug report GridLayout shows warnings for some child elements but in my case the ExtendedGridLayout shows warnings for all child elements. Also the bug report says that bug has been fixed in version 0.3.2 and I'm on version 2.1.2Tragus
Oh, sorry my poor english. I intended to say there is related issune. By that bug report, your issue seems to occured.Promiscuous

© 2022 - 2024 — McMap. All rights reserved.