Подтвердить что ты не робот

Разделители/разделители строк сетки андроида

Есть ли способ показать (горизонтальные) разделители между строками в gridview?

Я попытался поместить небольшое разделительное изображение под каждым элементом сетки, но это не решение, потому что оно не будет охватывать всю строку, если строка не заполнена предметами.

Есть ли способ добавить изображение между каждой строкой? Я могу найти методы для изменения пространства между строками.

4b9b3361

Ответ 1

Я закончил создание настраиваемого gridview, примерно так:

fooobar.com/questions/175065/...

используя фоновое изображение, которое точно так же высоко, как один элемент в моем gridview, и имеет devider внизу.

Работает как шарм!

Ответ 2

Если вы используете настраиваемый макет для элементов сетки. Ниже будет работать код.

Шаг 1: укажите цвет фона в GridView

Это будет служить разделителем.
Дайте 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" >

Шаг 2: укажите цвет фона в пользовательский макет элемента сетки

Это будет основным цветом для 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>

Результат

enter image description here

Примечание:
Если вы не хотите вертикальный разделитель, держите horizontalSpacing = 0dp
Если вам не нужен горизонтальный разделитель, держите verticalSpacing = 0dp

Ответ 3

Просто хотел поделиться тем, как я это сделал, используя ссылку, принятую 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 только для того, чтобы быть в безопасности, но я не думаю, что это имело бы значение в этом случае.

Ответ 4

Я предлагаю сделать следующее:

`

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