2010-07-07 67 views
0

我正试图在网格上方和下方显示图像。这是带有顶部徽标的初始屏幕,4个按钮用作网格视图,然后是底部的图像。用我目前的代码底部图像根本不显示。此外,我想将底部图像“bottomboxes”粘贴到显示屏的底部。如何在GridView上方和下方显示ImageView?

<ImageView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:paddingTop="20dp" 
android:src="@drawable/stuffbox" 
android:gravity="center"/> 

<GridView 
android:id="@+id/gridview" 
android:paddingTop="10dp" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:columnWidth="40dp" 
android:numColumns="2" 
android:verticalSpacing="10dp" 
android:horizontalSpacing="10dp" 
android:stretchMode="columnWidth" 
android:gravity="center"/> 

<ImageView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:src="@drawable/bottomboxes" 
android:paddingTop="20dp" 
/> 

</LinearLayout> 

回答

3

您当前的布局具有GridView控件设置为FILL_PARENT两个方向。这将强制gridview填充整个线性布局,并推开其他视图。

相反,你应该把:

<GridView 
android:id="@+id/gridview" 
android:paddingTop="10dp" 
android:layout_width="fill_parent" 
android:layout_height="0dp" 
android:layout_weight="1" 
android:columnWidth="40dp" 
android:numColumns="2" 
android:verticalSpacing="10dp" 
android:horizontalSpacing="10dp" 
android:stretchMode="columnWidth" 
android:gravity="center"/> 

线条机器人:layout_height = “0” 和android:layout_weight = “1” 告诉GridView控件,以填补在布局中剩余的可用空间,同时还允许imageviews有他们的地方。

+0

这工作得很好!我做出的唯一改变是改变 机器人:layout_height =“0” 到 机器人:layout_height =“WRAP_CONTENT” 不会再允许0 谢谢! – bdudaday 2010-07-07 22:15:53

+0

@bdudaday对不起,应该是0dp。使用0dp仍然是最好的方法。您不应将layout_height设置为网格视图上的wrap_content,因为它会尝试测量它的内容。设置为零可以节省数量惊人的CPU。 – CodeFusionMobile 2010-07-08 22:34:16

相关问题