扩展网格布局
Extending GridLayout
对于 GridLayout,这是一个有效的布局定义。没有关于的警告
'layout_height' attribute should be defined
或 'layout_width' attribute should be defined
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView />
</GridLayout>
另一方面,如果我扩展 GridLayout,等效布局会给出警告 'layout_height' attribute should be defined
和 'layout_width' attribute should be defined
<ExtendedGridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView />
</ExtendedGridLayout>
这是扩展网格布局的样子
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);
}
}
我试着浏览了 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
因此看起来应该基于继承,ExtendedGridLayout 还应该为其子项设置默认宽度和高度,或者执行 GridLayout 所做的任何操作以避免布局编辑器中出现警告消息。
所以我的问题是为什么 ExtendedGridLayout
有警告,我该如何预防?
这是 AndroidStudio 的默认行为。
避免该错误的一种方法是抑制。
<ExtendedGridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--suppress AndroidDomInspection -->
<ImageView />
</ExtendedGridLayout>
AndroidStudio 跳过显示 GridLayout 的错误,
但不会跳过 GridLayout 的子项。
这里是AndroidStudio的检查器the source code。
这里是related bug report.
根据此错误报告,您的问题发生在 this.
对于 GridLayout,这是一个有效的布局定义。没有关于的警告
'layout_height' attribute should be defined
或 'layout_width' attribute should be defined
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView />
</GridLayout>
另一方面,如果我扩展 GridLayout,等效布局会给出警告 'layout_height' attribute should be defined
和 'layout_width' attribute should be defined
<ExtendedGridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView />
</ExtendedGridLayout>
这是扩展网格布局的样子
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);
}
}
我试着浏览了 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
因此看起来应该基于继承,ExtendedGridLayout 还应该为其子项设置默认宽度和高度,或者执行 GridLayout 所做的任何操作以避免布局编辑器中出现警告消息。
所以我的问题是为什么 ExtendedGridLayout
有警告,我该如何预防?
这是 AndroidStudio 的默认行为。
避免该错误的一种方法是抑制。
<ExtendedGridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--suppress AndroidDomInspection -->
<ImageView />
</ExtendedGridLayout>
AndroidStudio 跳过显示 GridLayout 的错误, 但不会跳过 GridLayout 的子项。 这里是AndroidStudio的检查器the source code。
这里是related bug report.
根据此错误报告,您的问题发生在 this.