简体   繁体   English

AndroidGridView行分隔符/分隔符

[英]android gridview row dividers / separators

Is there a way to show (horizontal) dividers between rows in a gridview? 有没有办法显示网格视图中行之间的(水平)分隔线?

I tried putting a small divider-image below every grid item, but this is not a solution, because it won't span the whole row when a row is not completely filled with items. 我尝试在每个网格项下面放置一个小的分隔图像,但这不是解决方案,因为当一行未完全填充项时,它不会跨越整行。

Is there a way to just add an image between every row? 有没有一种方法可以在每行之间添加图像? I can only find methods for changing the space between rows. 我只能找到更改行间距的方法。

If you are using custom layout for grid items. 如果您对网格项目使用自定义布局。 Below code will work. 下面的代码将起作用。

Step 1: Give background color to GridView 步骤1:为GridView提供背景颜色

This is going to serve as a divider. 这将用作分隔线。
Give horizontalSpacing and verticalSpacing as 1dp horizontalSpacingverticalSpacing设为1dp
backgroundColor will be your divider color. backgroundColor将是您的分隔线颜色。

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

Step 2: Give background color to Custom Grid Item Layout 步骤2:为自定义网格项目布局提供背景颜色

This is going to serve as a foreground color for GridItems. 这将用作GridItems的前景色。
In my case I kept it white (#fff) 就我而言,我将其保持白色(#fff)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="#fff"
    android:padding="15dp"
     >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher_transparent" />

    <TextView
        android:id="@+id/lable"
        android:layout_marginTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textStyle="bold"
        android:textColor="#D0583B"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

Result 结果

在此处输入图片说明

Note: 注意:
If you do not want vertical separator, keep horizontalSpacing = 0dp 如果您不希望使用垂直分隔符,请保持horizontalSpacing = 0dp
If you do not want horizontal separator, keep verticalSpacing = 0dp 如果您不希望水平分隔符,请保持verticalSpacing = 0dp

I ended up creating a custom gridview, something like this: 我最终创建了一个自定义gridview,如下所示:

https://stackoverflow.com/a/9757501/1310343 https://stackoverflow.com/a/9757501/1310343

using a background image that is exactly as high as one item in my gridview, and has a devider at the bottom. 使用与我的gridview中的一个项目一样高的背景图像,并且在底部有一个devider。

Works like a charm! 奇迹般有效!

Just wanted to share how I implemented this using the link accepted by OP. 只是想分享我如何使用OP接受的链接来实现此目的。 For my case I also needed to control the length of the separators, so I couldn't get around subclassing GridView . 就我而言,我还需要控制分隔符的长度,因此无法绕开GridView子类。

public class HorizontalSeparatorGridView extends GridView {

    // Additional methods 

    @Override
    protected void dispatchDraw(Canvas canvas) {

        final int count = getChildCount();
        for(int i = 0; i < count; i++) {
            View child = getChildAt(i);
            int bottom = child.getBottom();
            int left = child.getLeft();
            int right = child.getRight();

            Paint paint = new Paint();
            paint.setColor(0xffececec);

            paint.setStrokeWidth(Math.round(0.5 * density));

            int offset = // Some offset

            canvas.drawLine(left + offset, bottom, right - offset, bottom, paint);
        }


        super.dispatchDraw(canvas);
    }

I subclassed dispatchDraw as opposed to onDraw just to be safe but I don't think it would matter in this case. 为了安全起见,我将dispatchDrawonDraw相对onDraw为子类,但是在这种情况下我认为这并不重要。

I suggest doing the following: 我建议您执行以下操作:

` `

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1sp"
        android:layout_marginLeft="7sp"
        android:layout_marginRight="7sp"
        android:layout_marginTop="7sp"
        android:background="@android:color/transparent">

        <TextView
            android:id="@+id/lblDeposit"
            android:layout_width="60sp"
            android:layout_height="40sp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="0sp"
            android:background="@drawable/rounded_top_left_rectangle"
            android:gravity="center"
            android:paddingLeft="5sp"
            android:scaleType="fitXY"
            android:text="Deposit"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000">
        </TextView>

        <TextView
            android:id="@+id/lblDepositvalue"
            android:layout_width="50sp"
            android:layout_height="40sp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="2sp"
            android:layout_marginRight="13sp"
            android:background="@drawable/rounded_top_right_rectangle"
            android:gravity="center_vertical|center_horizontal"
            android:scaleType="fitXY"
            android:text="40000/-Rs"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000">
        </TextView>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6sp"
        android:layout_marginLeft="7sp"
        android:layout_marginRight="7sp"
        android:layout_marginTop="2sp"
        android:background="@android:color/transparent">

        <TextView
            android:id="@+id/lblPoints"
            android:layout_width="60sp"
            android:layout_height="40sp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="0sp"
            android:background="@drawable/rounded_bottom_right_rectangle"
            android:gravity="center"
            android:paddingLeft="5sp"
            android:scaleType="fitXY"
            android:text="Points "
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000">
        </TextView>

        <TextView
            android:id="@+id/lblPointsValue"
            android:layout_width="50sp"
            android:layout_height="40sp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="2sp"
            android:layout_marginRight="13sp"
            android:background="@drawable/rounded_bottom_left_rectangle"
            android:gravity="center_vertical|center_horizontal"
            android:scaleType="fitXY"
            android:text="20"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000">
        </TextView>
    </TableRow>
</TableLayout>`  

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

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