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?
GridLayout is also not deprecated.
To add Components to a GridLayout You do not (can not) use the row and column to tell where to add the components -- add them in starting at the top left and going across the row first.
GridView simply gives us a two-dimensional view to display the items on the screen, under ViewGroup. On the other hand, GridLayout is a layout manager that arranges the views in a grid.
Parameters: rows - the rows, with the value zero meaning any number of rows. cols - the columns, with the value zero meaning any number of columns.
The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle. For example, the following is an applet that lays out six buttons into three rows and two columns:
A GridLayout class object presents with a grid of cells arranged in rows and columns. The class contains addWidget () method. Any widget can be added by specifying the number of rows and columns of the cell. Optionally, a spanning factor for row as well as column, if specified makes the widget wider or taller than one cell.
Click the Launch button to run GridLayoutDemo using Java™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index. The complete code of this demo is in the GridLayoutDemo.java file. A GridLayout object places components in a grid of cells.
GridView is a view, whereas GridLayout is a layout that can hold various views in it. Using GridView, you can display the items that are coming from the Adapter. GridLayout allows you to place many views and elements inside the layout and then style them. It allows you to set the number of rows and columns for your grid.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With