简体   繁体   English

GridView上的边距/填充顶部

[英]Margin/padding top on GridView

I have a problem with my GridView. 我的GridView有问题。 I am using part of Bitmapfun project, and I don't want to have a margin on the top. 我正在使用Bitmapfun项目的一部分,但我不想在顶部保留空白。

See this capture : 看到这个截图: 在此处输入图片说明

I have a large black area (where I put the 3 question marks) between the spinner and the first pictures. 在微调框和第一张图片之间,我有一个很大的黑色区域(在其中放置了3个问号)。

If I scroll down, I get a good render : photos slide under the spinner : 如果向下滚动,则会得到良好的渲染:照片在微调器下滑动: 在此处输入图片说明

If I go back to the top, I have again this large black area just before the first pics. 如果我回到顶部,那么在第一张照片之前,我还会有一个很大的黑色区域。 Someone could help me ? 有人可以帮助我吗?

Here is my gridview (image_grid_fragment.xml) : 这是我的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>

The GridView is included in this main layout : 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>

values/styles.xml : values / 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 : values-v11 / styles.xml:

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

ImageGridActivity.java : 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();
        }
    }
}

With layout bounds activated in development options, I have this result : 在开发选项中激活布局边界后,我得到以下结果:

在此处输入图片说明

What is the content of @layout/image_grid_fragment ? @layout/image_grid_fragment的内容是什么?

What if you try to change 如果您尝试更改该怎么办

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

to

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

Run your app on android 4.2 and in system settings, Developer Options -> show layout bounds (place checkmark). 在android 4.2上并在系统设置中的“开发人员选项”->“显示布局范围(选中标记)”上运行您的应用。 You will see if it's a padding or margin of some views. 您将看到它是某些视图的填充还是空白。

Also you might be set something wrong in bindView(or getView) for gridView adapter. 另外,对于gridView适配器,您可能在bindView(或getView)中设置了错误。

Thanks to Roger Alien !! 感谢罗杰·外星人! He put me on the right way ! 他让我走对了!

The problem was on the gridView adapter : 问题出在gridView适配器上:

Partial source code of ImageGridFragment.java : ImageGridFragment.java的部分源代码:

/**
 * The main adapter that backs the GridView. This is fairly standard except the number of
 * columns in the GridView is used to create a fake top row of empty views as we use a
 * transparent ActionBar and don't want the real top row of images to start off covered by it.
 */
private class ImageAdapter extends BaseAdapter {

    private final Context mContext;
    private int mItemHeight = 0;
    private int mNumColumns = 0;
    private int mActionBarHeight = 0;
    private GridView.LayoutParams mImageViewLayoutParams;

    public ImageAdapter(Context context) {
        super();
        mContext = context;
        mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        // Calculate ActionBar height
        TypedValue tv = new TypedValue();
        if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            mActionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
        }
    }

    @Override
    public int getCount() {
        // If columns have yet to be determined, return no items
        if (getNumColumns() == 0) {
            return 0;
        }

        // Size + number of columns for top empty row
        return Images.imageThumbUrls.length + mNumColumns;
    }

    @Override
    public Object getItem(int position) {
        return position < mNumColumns ?
                null : Images.imageThumbUrls[position - mNumColumns];
    }

    @Override
    public long getItemId(int position) {
        return position < mNumColumns ? 0 : position - mNumColumns;
    }

    @Override
    public int getViewTypeCount() {
        // Two types of views, the normal ImageView and the top row of empty views
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return (position < mNumColumns) ? 1 : 0;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        // First check if this is the top row
        if (position < mNumColumns) {
            if (convertView == null) {
                convertView = new View(mContext);
            }
            // Set empty view with height of ActionBar
            convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight));
            //
            // THE PROBLEM IS HERE !!
            //

            return convertView;
        }

        // Now handle the main ImageView thumbnails
        ImageView imageView;
        if (convertView == null) { // if it's not recycled, instantiate and initialize
            imageView = new RecyclingImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(mImageViewLayoutParams);
        } else { // Otherwise re-use the converted view
            imageView = (ImageView) convertView;
        }

        // Check the height matches our calculated column width
        if (imageView.getLayoutParams().height != mItemHeight) {
            imageView.setLayoutParams(mImageViewLayoutParams);
        }

        // Finally load the image asynchronously into the ImageView, this also takes care of
        // setting a placeholder image while the background thread runs
        mImageFetcher.loadImage(Images.imageThumbUrls[position - mNumColumns], imageView, false);
        return imageView;
    }

    /**
     * Sets the item height. Useful for when we know the column width so the height can be set
     * to match.
     *
     * @param height
     */
    public void setItemHeight(int height) {
        if (height == mItemHeight) {
            return;
        }
        mItemHeight = height;
        mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
        mImageFetcher.setImageSize(height);
        notifyDataSetChanged();
    }

    public void setNumColumns(int numColumns) {
        mNumColumns = numColumns;
    }

    public int getNumColumns() {
        return mNumColumns;
    }
}

Look in the middle, in public View getView() : Bitmapfun project adds an empty view with a height equal to the height of ActionBar (because the actionbar is visible on the Bitmapfun original project). 在公共视图getView()的中间查看:Bitmapfun项目添加一个高度等于ActionBar高度的空视图(因为该操作栏在Bitmapfun原始项目上可见)。

If we comment this line (or if we let mActionBarHeight to 0 in public ImageAdapter()), we dont have this space before first pictures. 如果我们注释此行(或者如果在公共ImageAdapter()中将mActionBarHeight设置为0),则在第一张图片之前没有此空间。

Thanks to Tim Castelijns too .! 还要感谢Tim Castelijns。!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM