【发布时间】:2012-08-20 00:02:30
【问题描述】:
有没有办法在 gridview 中的行之间显示(水平)分隔线?
我尝试在每个网格项目下方放置一个小的分隔图,但这不是解决方案,因为当一行没有完全填满项目时,它不会跨越整行。
有没有办法在每一行之间添加一张图片?我只能找到改变行间距的方法。
【问题讨论】:
-
这里有一个类似的帖子,里面有代码和建议:stackoverflow.com/questions/7132030/…
有没有办法在 gridview 中的行之间显示(水平)分隔线?
我尝试在每个网格项目下方放置一个小的分隔图,但这不是解决方案,因为当一行没有完全填满项目时,它不会跨越整行。
有没有办法在每一行之间添加一张图片?我只能找到改变行间距的方法。
【问题讨论】:
只是想分享我是如何使用 OP 接受的链接实现的。
对于我的情况,我还需要控制分隔符的长度,所以我无法绕过子类化 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);
}
为了安全起见,我将 dispatchDraw 子类化为 onDraw 的子类,但我认为在这种情况下并不重要。
【讨论】:
如果您对网格项目使用自定义布局。下面的代码将起作用。
这将用作分隔线。
将horizontalSpacing 和verticalSpacing 设为1dp
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" >
这将用作 GridItems 的前景色。
就我而言,我将其保持为白色(#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>
注意:
如果您不想要垂直分隔符,请保留horizontalSpacing = 0dp
如果你不想要水平分隔符,请保留verticalSpacing = 0dp
【讨论】:
我最终创建了一个自定义网格视图,如下所示:
https://stackoverflow.com/a/9757501/1310343
使用与我的网格视图中的一个项目完全一样高的背景图像,并且在底部有一个分隔符。
像魅力一样工作!
【讨论】:
我建议执行以下操作:
`
<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>`
【讨论】: