android中九宫格布局与gridview
纵观现在的应用程序,九宫格是非常常见的一种布局方式。很多优秀的手机应用程序都采用了这一布局。下面就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);
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。