android - android中的动态gridview分隔符

标签 android gridview separator

在我的应用程序中,我必须根据用户交互动态更改 gridview 分隔符(例如,水平分隔符为红色,1px 为;垂直分隔符为蓝色,2px 为...)我该怎么做?

我试过这种方法 Android GridView with Separator ,但这对我不利,因为我需要不同颜色的线性和垂直分隔符。

我也看到了这个例子Android GridView draw dividers ,但我不知道如何以编程方式进行(没有 xml 文件)。

有什么帮助吗?

最佳答案

试试这个:

GridView grid = (GridView) findViewById(R.id.grid);
ArrayAdapter<String> a = new ArrayAdapter<String>(this, R.layout.item);
a.add("0"); a.add("1"); a.add("2");
a.add("3"); a.add("4"); a.add("5");
a.add("6"); a.add("7"); a.add("8");
a.add("9");
grid.setAdapter(a);

item.xml 在哪里:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/iten"
    android:textSize="30dip"
    android:textStyle="bold"
    android:paddingLeft="10dip"
/>

GridView 定义为:

<GridView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/grid"
    android:numColumns="3"
    android:background="@drawable/grid"
    android:listSelector="@null"
/>

最重要的是两个drawable:item.9.png 和grid.9.png

item.9.png 是 here

grid.9.png 是 here

结果 GridView 如下所示

enter image description here

关于android - android中的动态gridview分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16272067/

相关文章:

java - RxAndroid,结合多个网络调用

android - 开始使用 sugarORM 1.4

arrays - 使用自定义分隔符在 hive 中导入复杂的数据结构

iphone - UITableView tableHeaderView 缺少分隔符

c# - 是否可以以编程方式隐藏单选按钮列表的特定列表项?

android - 用标题分隔 Android 列表

android - ViewPager 中的 ScrollView 定位不正确

java - 出现错误 :- unfortunately app has stopped in android

c# - 当文本很长且换行时,gridview 边界字段宽度不受控制

asp.net - 面临错误 DataSource 和 And DataSourceID 均在 GridView Entity Framework 上定义