android - GridView 上的边距/填充顶部

标签 android android-gridview android-gridlayout

我的 GridView 有问题。我正在使用 Bitmapfun 项目的一部分,我不想在顶部留有边距。

查看此截图: enter image description here

我在微调器和第一张图片之间有一个大的黑色区域(我在其中放置了 3 个问号)。

如果我向下滚动,我会得到一个很好的渲染:照片在微调器下方滑动: enter image description here

如果我回到顶部,我又会在第一张照片之前看到这个大的黑色区域。 有人可以帮助我吗?

这是我的 GridView (image_grid_fragment.xml):

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView"
    style="@style/PhotoGridLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="@dimen/image_thumbnail_size"
    android:horizontalSpacing="@dimen/image_thumbnail_spacing"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="@dimen/image_thumbnail_spacing" >

</GridView>

GridView 包含在此主布局中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="17dp"
        android:layout_marginLeft="16dp"
        android:text="Album : "
        android:textAppearance="?android:attr/textAppearanceSmallPopupMenu"
        android:textSize="18dp" />

    <Spinner
        android:id="@+id/spinner_album"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/TextView1"
        android:layout_marginTop="12dp"
        android:inputType="text"
        android:textSize="18dp" />

    <include layout="@layout/image_grid_fragment" />

</RelativeLayout>

值/styles.xml :

<style name="PhotoGridLayout">
    <item name="android:drawSelectorOnTop">false</item>
    <item name="android:listSelector">@drawable/photogrid_list_selector</item>
</style>

values-v11/styles.xml :

<style name="PhotoGridLayout">
    <item name="android:drawSelectorOnTop">true</item>
</style>

ImageGridActivity.java :

public class ImageGridActivity extends FragmentActivity {
    private static final String TAG = "ImageGridFragment";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
            final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(android.R.id.content, new ImageGridFragment(), TAG);
            ft.commit();
        }
    }
}

在开发选项中激活布局边界后,我得到了这个结果:

enter image description here

最佳答案

@layout/image_grid_fragment 的内容是什么?

如果你尝试改变会怎样

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView"
    style="@style/PhotoGridLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   ...

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView"
    style="@style/PhotoGridLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   ...

在 android 4.2 和系统设置中运行您的应用程序,开发人员选项 -> 显示布局边界(放置复选标记)。您会看到它是某些 View 的填充还是边距。

此外,您可能在 gridView 适配器的 bindView(或 getView)中设置了错误。

关于android - GridView 上的边距/填充顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21735654/

相关文章:

java - GridView setAdapter 方法给出 NullPointerException

android - 为什么我的适配器的 getView() 执行无限次?

android - 帮助!如何为 Android 创建日历应用程序?

android - 为什么 gridlayout 中的多行按钮有奇怪的上边距?

android - 我怎样才能在 GridView android 中获得垂直滚动?

android - 如何更改 Android 中按下事件的文本颜色?

android - ExoPlayer 无法从文件中播放

android - 如何连接到谷歌播放服务和加载排行榜

android - 为什么我不应该在 android 中使用 System.out.println()

Android:在每个 ListView 行中添加两个 GridView