【问题标题】:Adding a divider between rows of GridView在 GridView 的行之间添加分隔线
【发布时间】:2016-05-30 06:24:25
【问题描述】:

我使用 GridView 制作了一个数据表,并希望在每行下方应用水平分隔线,如 Google 材料设计指南中所示。但是,我找不到有关其实施的任何信息。有任何想法吗?

【问题讨论】:

标签: android android-layout android-view android-gridview android-tablelayout


【解决方案1】:

尝试像这样为 GridView 赋予背景颜色

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#e5e5e5"
    android:horizontalSpacing="1dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="1dp" >

以dp为单位给出horizo​​ntalSpacing和verticalSpacing的大小 并给出你想要作为分隔符的背景颜色 喜欢灰色

【讨论】:

  • 这只是使整个 gridView 的颜色指定为背景。
【解决方案2】:

使用GridViewandroid:horizontalSpacing 属性。

【讨论】:

    【解决方案3】:

    只需像这样创建自定义网格视图

    package net.nightwhistler.pageturner.view;
    
    import net.nightwhistler.pageturner.R;
    import net.nightwhistler.pageturner.library.LibraryBook;
    import net.nightwhistler.pageturner.library.QueryResult;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.view.KeyEvent;
    import android.widget.GridView;
    
    public class BookCaseView extends GridView {
    
    private Bitmap background;
    
    private int mShelfWidth;
    private int mShelfHeight;   
    
    private QueryResult<LibraryBook> result;    
    
    private LibraryBook selectedBook;   
    
    public BookCaseView(Context context, AttributeSet attributes) {
        super(context, attributes);
    
        this.setFocusableInTouchMode(true);
        this.setClickable(false);
    
        final Bitmap shelfBackground = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.shelf_single);
        setBackground(shelfBackground);
        this.setFocusable(true);
    }
    
    public void setBackground(Bitmap background) {
        this.background = background;
    
        mShelfWidth = background.getWidth();
        mShelfHeight = background.getHeight();
    }   
    
    protected void onClick( int bookIndex ) {
        LibraryBook book = this.result.getItemAt(bookIndex);
    
        this.selectedBook = book;
        invalidate();
    }
    
    @Override
    protected void dispatchDraw(Canvas canvas) {
        final int count = getChildCount();
        final int top = count > 0 ? getChildAt(0).getTop() : 0;
        final int shelfWidth = mShelfWidth;
        final int shelfHeight = mShelfHeight;
        final int width = getWidth();
        final int height = getHeight();
        final Bitmap background = this.background;
    
        for (int x = 0; x < width; x += shelfWidth) {
            for (int y = top; y < height; y += shelfHeight) {
                canvas.drawBitmap(background, x, y, null);
            }
    
            //This draws the top pixels of the shelf above the current one
    
            Rect source = new Rect(0, mShelfHeight - top, mShelfWidth, mShelfHeight);
            Rect dest = new Rect(x, 0, x + mShelfWidth, top );              
    
            canvas.drawBitmap(background, source, dest, null);            
        }        
    
    
        super.dispatchDraw(canvas);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ( keyCode == KeyEvent.KEYCODE_BACK && this.selectedBook != null ) {
            this.selectedBook = null;
            invalidate();
            return true;
        }
    
        return false;
    }   
    

    }

    我从她那里得到了他的回答 background scrolling with item in gridview

    【讨论】: