android - Android 中的 GridView 行背景图片

标签 android gridview background row

我想为 GridView 的所有行设置背景图像。 我使用了这段代码:

GridView gv = (GridView) findViewById(R.id.grid);

    ArrayAdapter<String> aa = new ArrayAdapter<String>(
            this,
            R.layout.row,
            items);

    gv.setAdapter(aa);
    gv.setOnItemClickListener(this);

main.xml:(我这里设置GridView设计)

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 

   >
<TextView
    android:id="@+id/selection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
  android:textSize="14pt"
  android:textStyle="bold"


    />
<GridView
    android:id="@+id/grid"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:verticalSpacing="65px"
    android:horizontalSpacing="5px"
    android:numColumns="auto_fit"
    android:columnWidth="100px"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:fastScrollEnabled="true"
    />

row.xml:(我这里设置了Grid View 设计的行数)

<LinearLayout
 xmlns:android = "http://schemas.android.com/apk/res/android"
 android:layout_width  = "fill_parent"
 android:layout_height = "wrap_content"
 android:orientation   = "horizontal" 
 android:background="@drawable/rowbg"
 >


<TextView
android:id = "@+id/label"
android:layout_width  = "wrap_content"
android:layout_height = "wrap_content"
android:textSize      = "40sp" />

当我运行项目时,它会强制关闭程序。但是当我如下更改第二行时,它运行良好,当然没有任何行设计:

ArrayAdapter<String> aa = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            items);

我的代码的哪一部分应该更改?

谢谢


用eclipse运行程序后会出现一个对话框: “应用程序异常停止,请重试。” 在 LogCat 中出现此消息: “06-21 12:05:25.724: E/dalvikvm(305): 无法打开堆栈跟踪文件 '/data/anr/traces.txt': 权限被拒绝”

我也尝试在 Galaxy S 上运行应用程序,但我看到了同样的错误。

最佳答案

创建一个扩展 ArrayAdapter 的适配器类。

在该类的getView方法中,为row填充布局,访问要设置数据的textview。

关于android - Android 中的 GridView 行背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11116831/

相关文章:

css - 标题上的透明框

android - 此范围关系未授权访问 token

android - 从 webview 中的 pdf 文件的超链接加载 pdf 查看器

android - 什么布局用于绘制类似垄断的游戏板

c# - 用仅包含数值的行填充 Gridview 的查询没有给我想要的结果

macos - macOS 上的 SwiftUI ListStyle

java - Activity 和 Fragment 并发

android - 将距离 AGGREGATE_DISTANCE_DELTA 插入 Google 健身

asp.net - Gridview 中的条件删除按钮

css - Rails 应用程序中 CSS "background:url"属性的正确语法是什么?