android gridview行分隔符/分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12109126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android gridview row dividers / separators
提问by Frank
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.
有没有办法在每一行之间添加一个图像?我只能找到改变行间距的方法。
采纳答案by Frank
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.
使用与我的网格视图中的一个项目完全一样高的背景图像,并且底部有一个分隔符。
Works like a charm!
奇迹般有效!
回答by Chaitanya Chandurkar
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
backgroundColor will be your divider color.
这将用作分隔符。
GivehorizontalSpacing
和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" >
Step 2: Give background color to Custom Grid Item Layout
步骤 2:为自定义网格项目布局提供背景颜色
This is going to serve as a foreground color for GridItems.
In my case I kept it white (#fff)
这将用作 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>
Result
结果
Note:
If you do not want vertical separator, keep horizontalSpacing = 0dp
If you do not want horizontal separator, keep verticalSpacing = 0dp
注意:
如果你不想要垂直分隔符,保留horizontalSpacing = 0dp
如果你不想要水平分隔符,保留verticalSpacing = 0dp
回答by jrhee17
Just wanted to share how I implemented this using the link accepted by OP.
For my case I also needed to control the length of the separators, so I couldn't get around subclassing GridView
.
只是想分享我如何使用 OP 接受的链接来实现这一点。对于我的情况,我还需要控制分隔符的长度,所以我无法绕过 subclassing 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.
我子类化dispatchDraw
而不是为了onDraw
安全,但我认为在这种情况下这无关紧要。
回答by Snehal Poyrekar
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>`