创建静态网格视图

时间:2016-01-13 10:18:06

标签: android gridview

我想在我的应用中创建静态网格视图。它应该是5X5维度。我知道如何构建它,但我不明白的是,我想将一个蓝色图像膨胀到网格的所有行和列。我无法弄清楚如何实现这一目标。我认为如果我们使用适配器,我们应该有一个图像列表,但我只有一个图像。

1 个答案:

答案 0 :(得分:1)

  

如果你只想用5 X 5渲染相同的图像。

创建activity_main.xml

<RelativeLayout 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:orientation="vertical"

    tools:context=".MainActivity" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:numColumns="5" >

    </GridView>

   <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textStyle="bold"
        android:text=" Computer Languages..." />

</RelativeLayout>
来自MainActivity

来电

gv=(GridView) findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(this));

创建CustomAdapter。

public class CustomAdapter extends BaseAdapter {
    int count = 10;
    Context context;
    private static LayoutInflater inflater=null;
    public CustomAdapter(MainActivity mainActivity) {
        // TODO Auto-generated constructor stub
        context=mainActivity;
        inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return count;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View rowView = inflater.inflate(R.layout.image_list, null);
        return rowView;
    }

}

及其布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_gravity="center"
        android:layout_width="88dp"
        android:layout_height="88dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:src="@drawable/ic_launcher" />

</LinearLayout>