【问题标题】:Drawable dividers for a GridViewGridView 的可绘制分隔线
【发布时间】:2016-03-09 11:40:53
【问题描述】:

如何为 GridView 设置自定义分隔线?
我的意思是,有一个带有蓝色渐变分隔线的 .png 项目,但是如何将它放在 GridView 中的正确位置?

通过互联网搜索并没有带来任何可行的结果..

期待

现实

gridview 包装器 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_blue_dark"
android:orientation="vertical">

....

<GridView
    android:id="@+id/gv_stock"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchMode="columnWidth"
    android:numColumns="2"/>
</LinearLayout>

gridview 项目 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_blue_dark"
style="@style/padded_layout"
android:weightSum="2">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        style="@style/teal_text_middle"
        android:gravity="center"
        android:text="test"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1"
    android:gravity="center">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.7"/>
        <ImageView
            android:id="@+id/iv_arrow"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom"
            android:layout_weight="1.3"
            android:src="@drawable/ic_arrow_green"/>
    </LinearLayout>

    <TextView
        android:id="@+id/tv_value"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        style="@style/white_text_big"
        android:gravity="center"
        android:text="test"/>
</LinearLayout>

</LinearLayout>

【问题讨论】:

  • @BobMalooga,你的意思是这样吗? android:background="@drawable/ic_separator_horizontal"。这适用于水平渐变线。但是如何在列项之间插入这样的内容?渐变应该只延伸到一行。
  • 是的,我明白了。但这是一种解决方法。这将很难创建恰好位于每行中间的渐变。我的意思是它不适合所有屏幕尺寸。
  • @BobMalooga,如果您以这种解决问题的方式发布答案,我将不胜感激。因为我真的不知道如何使用这两个较小的渐变

标签: android gridview android-gridview divider


【解决方案1】:

使用GridLayoutManagerRecyclerview 并从以下gist 添加Itemdecoration。您可以根据需要更改偏移量。

【讨论】:

  • 谢谢,通过几处修改提供了预期的行为
【解决方案2】:

通过使用这个答案here 并简单地更改像这样的图像的灰色纯色背景颜色(我将它设置为 hdpi 分辨率 - 但它应该可以很好地上下拉伸),

你应该得到这样的效果(这里是在一个 ldpi 设备上,所以按比例缩小了两倍):

这与您正在寻找的东西相距不远。

至少在原则上有效。它可能需要一些调整(缩小垂直中心部分),但你明白了这个概念。 您可能还想覆盖一些顶部(也可能是底部)部分,以便隐藏技巧。

通过简单的“透视”效果,您可以避免混淆样式/主题。


[编辑]

是的,我知道,这是 MAINZ,而不是 MEINZ(但是,嗯……发音确实是一样的!快点是一个糟糕的顾问,你知道的)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 2015-07-20
    • 2014-12-18
    • 1970-01-01
    相关资源
    最近更新 更多