如何自定义android GridView Text?

时间:2014-04-09 13:58:26

标签: android android-layout android-gridview android-gridlayout

我正在处理一个项目,我一直在努力自定义网格视图文本。我在不同的网站上搜索过,但我找不到如何自定义文本的例子。

我希望能够添加间距,更改颜色以及更改行的背景。 如果有人有教程或有一个有效的代码,我会非常感激。

提前致谢

1 个答案:

答案 0 :(得分:0)

执行单个项目布局和网格布局,然后在适配器上为每个内容增加项目布局,这样它们就会坚持到网格布局中。

通过这种方式,您可以编辑单个项目布局,并使其显示为您想要的内容。

我会给你一个关于C#的例子,但很容易就可以在Java上使用它。

//网格布局

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="400dp"
    android:numColumns="4"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:background="#444444" />

//项目布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/image"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop" />
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="#88FFFFFF"
        android:gravity="center_horizontal">
        <TextView
            android:id="@+id/name"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textStyle="bold"
            android:textSize="25dp"
            android:textColor="#000000" />
    </LinearLayout>
</RelativeLayout>

//在网格适配器类

// create a new ImageView for each item referenced by the Adapter
public override View GetView (int position, View convertView, ViewGroup parent)
{
    View grid;

    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater)context
            .GetSystemService (Context.LayoutInflaterService);
        grid = inflater.Inflate (Resource.Layout.gridItem, null);
    }
    else    {

        grid = (View) convertView;
    }

    ImageView image = (ImageView)grid.FindViewById (Resource.Id.image);
    TextView name= (TextView)grid.FindViewById (Resource.Id.name);

    image.SetImageBitmap(DecodeSampledBitmap ("Image Path", Width, Height));
    name.Text = "Text You Want";

    return grid;
}
相关问题