码迷,mamicode.com
首页 > 移动开发 > 详细

android中九宫格布局与gridview

时间:2015-06-07 09:37:52      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:android开发   android   应用程序   gridview   layout   

纵观现在的应用程序,九宫格是非常常见的一种布局方式。很多优秀的手机应用程序都采用了这一布局。下面就android中九宫格布局方式的实现和大家做一个简单的介绍。

首先在youxi.xml的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
      <TextView 
            android:layout_width="50dp"
    android:layout_height="45dp"
    android:gravity="center"
            android:id="@+id/textview" 
            android:background="@drawable/sanguosss"
            /> 
</RelativeLayout>

然后是gridview.xml的布局

<?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:gravity="center"
    android:background="@drawable/biaoge" 
    >
    <GridView
    android:layout_width="200dp"
    android:layout_height="400dp"


    android:numColumns="5"
    android:id="@+id/gridview"
    android:horizontalSpacing="0dp"
         android:verticalSpacing="0dp"
     />
<!--       android:layout_marginTop="5dp" -->
</LinearLayout>

最后是Adapter.java的内容

package cn.zs.service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import cn.zs.lianliankan.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;


public class youxi extends Activity {
private String[] text={"啊","放","啊","放","啊","放","放","啊",
"啊","放","啊","放","啊","放","啊","放","啊","放","啊","放","啊","放",
"放","啊","放","啊","放","啊","放","啊","放","啊","啊","放","啊","啊",
"放","啊","放","啊"};
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
GridView gridView=(GridView) findViewById(R.id.gridview);
List<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>();
for(int i=0;i<text.length;i++){
HashMap<String, Object> item=new HashMap<String, Object>();
item.put("name",text[i]);
data.add(item);
}
SimpleAdapter simpleAdapter=new SimpleAdapter(getApplicationContext(), data, R.layout.youxi, new String[]{"name"},new int[]{R.id.textview});
gridView.setAdapter(simpleAdapter);

}


}

技术分享

android中九宫格布局与gridview

标签:android开发   android   应用程序   gridview   layout   

原文地址:http://blog.csdn.net/u014535666/article/details/46391923

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!