扩展GridLayout

时间:2016-07-13 02:21:45

标签: android android-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>

这是扩展的gridlayout看起来像

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来源,看起来他们所做的就是扩展ViewGroup.LayoutParams并设置默认的宽度和高度,就像PercentRelativeLayout

一样

所以看起来基于继承,ExtendedGridLayout也应该为它的子节点设置默认的宽度和高度,或者做GridLayout做的任何事情来避免布局编辑器中的警告消息。

所以我的问题是为什么ExtendedGridLayout有警告,我该如何预防呢?

1 个答案:

答案 0 :(得分:2)

这是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