【问题标题】:Extending GridLayout扩展 GridLayout
【发布时间】:2016-07-27 08:08:33
【问题描述】:

对于 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 源代码,他们所做的似乎是扩展 ViewGroup.LayoutParams 并设置默认宽度和高度,就像 PercentRelativeLayout 一样

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

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

【问题讨论】:

  • 我个人更喜欢使用LinearLayouts 创建自己的视图,而不是使用无用的GridLayout
  • 是干什么用的?你为什么不想给它宽度和高度?
  • @uval gridview 设置它的孩子的宽度和高度。尤其是当您明确说明列数和行数时
  • 我不明白你为什么在 xml 中添加了一个空的 ImageView。是伪代码吗?这是你的真实代码吗?或者是偶然的? (我从未见过在 xml 中如此使用完全空视图)
  • @uval 这是一个精简的版本来显示我遇到的问题

标签: android android-gridlayout


【解决方案1】:

这是 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 显示了一些子元素的警告,但在我的情况下,ExtendedGridLayout 显示了所有子元素的警告。错误报告还说该错误已在 0.3.2 版中修复,而我使用的是 2.1.2 版
  • 哦,对不起我的英语不好。我的意思是说有相关的问题。通过该错误报告,您的问题似乎发生了。
猜你喜欢
  • 2018-02-22
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
  • 2019-02-25
  • 2020-09-10
  • 2014-09-23
  • 2020-12-11
  • 1970-01-01
相关资源
最近更新 更多