【发布时间】: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