简体   繁体   English

Android中的GridView行背景图像

[英]GridView row background image in Android

I want to set background image to all rows of a GridView. 我想将背景图像设置为GridView的所有行。 I used this code: 我用过这段代码:

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: (I set GridView design here) 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: (I set rows of Grid view design here) row.xml :(我在这里设置了网格视图设计的行)

<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" />

When i run project it forces to close program. 当我运行项目时,它强制关闭程序。 But when I change second row as below, it runs well, ofcorse without any design for rows: 但是当我改变第二行时,它运行良好,没有任何行设计:

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

Which part of my code should be changed? 我的代码中应该更改哪一部分?

Thanks 谢谢


After running the program with eclipse a dialoge appears: "The application has stopped unexceptedly. Please try again." 在使用eclipse运行程序后,会出现一个拨号:“应用程序已无法停止。请再试一次。” and in LogCat this message appears: "06-21 12:05:25.724: E/dalvikvm(305): Unable to open stack trace file '/data/anr/traces.txt': Permission denied" 在LogCat中出现此消息:“06-21 12:05:25.724:E / dalvikvm(305):无法打开堆栈跟踪文件'/data/anr/traces.txt':权限被拒绝”

I also try running application on Galaxy S, and I see the same error. 我也尝试在Galaxy S上运行应用程序,我看到同样的错误。

Create an adapter class which extends ArrayAdapter. 创建一个扩展ArrayAdapter的适配器类。

In the getView method of this class, inflate the layout for row, access the textview to which the data should be set. 在此类的getView方法中,为行的布局增加,访问应将数据设置到的textview。

A good solution is to subclass the gridview as described here: 一个好的解决方案是将gridview子类化,如下所述:

http://erikw.eu/android-row-background-in-gridview/ http://erikw.eu/android-row-background-in-gridview/

and reproduced below: 并转载如下:

public class MyGridView extends GridView {

    private Bitmap background;

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        background = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
    }


   @Override
    protected void dispatchDraw(Canvas canvas) {
        int count = getChildCount();
        int top = count &gt; 0 ? getChildAt(0).getTop() : 0;
        int backgroundWidth = background.getWidth();
        int backgroundHeight = background.getHeight();
        int width = getWidth();
        int height = getHeight();

        for (int y = top; y &lt; height; y += backgroundHeight){
            for (int x = 0; x &lt; width; x += backgroundWidth){
                canvas.drawBitmap(background, x, y, null);
            }
        }

        super.dispatchDraw(canvas);
    }
}

usage 用法

<your.package.name.MyGridView
    android:id="@+id/mygridview"
    <!-- other GridView configuration parameters -->
    />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM