支持库中的GridLayout在Android 2上无法显示2行,onChildVisibilityChanged错误。

10
有人成功使支持库在Android 2中正确呈现网格布局吗?屏幕上只显示一行,而不是两行和两列,并且在logcat输出中看到以下错误:Android GridLayout Could not find method android.support.v7.widget.ViewGroup.onChildVisibilityChanged。
当我将布局标签从android.support.v7.widget.GridLayout更改为GridLayout时,完全相同的布局在Android4->ICS上工作正常。
这可能与设置有关吗? 我在Eclipse项目属性的Android选项卡中具有gridlayout_v7库项目,并且v.13 jar位于构建路径上。
下面贴出了XML布局,它失败了。 我在图像按钮标记中显式添加了布局行和列,以尝试解决问题。 如果有人拥有在支持库下运行的Android 2上的可用示例,请分享。
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"  android:rowCount="2"
android:gravity="center_vertical" 
android:layout="@drawable/bg_test_main" >

<ImageButton android:id="@+id/btnSentence"
android:layout_row="0"
android:layout_column="0"
android:src="@drawable/testa_btn"
android:contentDescription="@string/spin_fill_in"
android:background="@android:color/transparent"
/>
<ImageButton android:id="@+id/btnAudio"
android:layout_row="0"
android:layout_column="1"
    android:src="@drawable/testb_btn"
android:contentDescription="@string/audio_quiz"
android:background="@android:color/transparent"
/>
<ImageButton android:id="@+id/btnPickWord"
android:layout_row="1"
android:layout_column="0"
android:src="@drawable/testc_btn"
android:background="@android:color/transparent"
android:contentDescription="@string/def_pick_word" />

<ImageButton android:id="@+id/btnPickDef"
android:layout_row="1"
android:layout_column="1"
android:src="@drawable/testd_btn"
android:background="@android:color/transparent"
android:contentDescription="@string/pick_def" />

</android.support.v7.widget.GridLayout>
1个回答

37

我猜你忘了添加XML命名空间。请按照以下方式进行更正:

<android.support.v7.widget.GridLayout 
     xmlns:grid="http://schemas.android.com/apk/res-auto"
     xmlns:android="http://schemas.android.com/apk/res/android">
...
</android.support.v7.widget.GridLayout>

还要不要忘记给兼容GridLayout使用的属性加上XML命名空间前缀:

<ImageButton android:id="@+id/btnSentence"
    grid:layout_row="0"
    grid:layout_column="0"
    ...
/>

希望能对你有所帮助...


这是一个很棒的答案!谢谢Tomas!你有任何文档链接吗?在创建这个帖子之前,我做了很多搜索。现在布局有两行了。下一个问题是让网格在上下左右具有相等的边距居中。你也有解决方法吗? - Beth Mezias
@Beth,如果他回答了你,你应该检查一下这个答案。 - confucius
啊 - 点击复选框...当这个答案出现时,我试图弄清楚它。感谢您@Nammari - Beth Mezias
将空间对象添加到布局后,我发现GridLayout的工作方式与我的预期不同。虽然我已经使其正常工作了,但它就像是一个FrameLayout,锚定在右上角。在更多的示例和文档可用之前,我已经回滚到使用weighted和nested LinearLayouts。 - Beth Mezias
为什么在GridLayout的属性中,我得到的是app:rowCount而不是android:rowCount - mr5
@mr5,你有“xmnls:app”吗? - Erik B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接