【问题标题】:How to display an ImageView above and below a GridView?如何在 GridView 上方和下方显示 ImageView?
【发布时间】:2010-07-07 21:33:05
【问题描述】:

我正在尝试在网格上方和下方显示图像。这是初始屏幕,顶部有徽标,4 个按钮作为网格视图,然后是底部图像。使用我当前的代码,底部图像根本不显示。另外我想将底部图像“底部框”粘贴到显示器的底部。

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

【问题讨论】:

    标签: android xml


    【解决方案1】:

    您当前的布局将 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"/> 
    

    android:layout_height="0" 和 android:layout_weight="1" 行告诉 gridview 填充布局中剩余的可用空间,同时仍然允许 imageviews 占有一席之地。

    【讨论】:

    • 这很好用!我所做的唯一更改是将 android:layout_height="0" 更改为 android:layout_height="wrap_content" 它不允许为 0。再次感谢!
    • @bdudaday 抱歉,应该是 0dp。使用 0dp 仍然是最好的方法。您不应该在网格视图上将 layout_height 设置为 wrap_content,因为它会尝试测量其内容。设置为零可以节省大量 CPU。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2016-05-23
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多