GridView是一个表格视图,应用在表格的布局里面非常的方面,基本简单的应用代码如下:
下面的是item的布局
效果图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.aeckj.distribution.MainActivity"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_address"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#A7FA17"
/>
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"/>
</LinearLayout>
主页面的布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的规划:"/>
android:id="@+id/sv_myPlan"
android:layout_width="match_parent"
android:layout_height="wrap_content"> -->
<GridView
android:id="@+id/gv_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="20dp"
android:verticalSpacing="20dp"
android:horizontalSpacing="20dp"
android:numColumns="3"
android:gravity="center"
>
</GridView>
<pre name="code" class="html"> </LinearLayout>
</pre><pre code_snippet_id="1718721" snippet_file_name="blog_20160616_4_9675646" name="code" class="html">执行代码:
<pre name="code" class="java">
private String[] itemTexts = new String[] { "北京市东城区港澳中心AAAAAA", "北京市东城区东四十条港澳中心203", "北京市东城区东四十条港澳中心204", "北京市东城区东四十条港澳中心205", "北京市东城区东四十条港澳中心206", "北京市东城区东四十条港澳中心207", "北京市东城区东四十条港澳中心208", "北京市东城区东四十条港澳中心209" };
private int[] itemImages = new int[] { R.drawable.enter, R.drawable.enter, R.drawable.enter,
R.drawable.enter, R.drawable.enter, R.drawable.enter, R.drawable.enter,
R.drawable.enter };
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_path_planning);
lv_panning = (ListView) findViewById(R.id.lv_panning);
// sv_myPlan = (ScrollView) findViewById(R.id.sv_myPlan);
gv_parent = (GridView) findViewById(R.id.gv_parent);
adapterGraid= new MyGridViewAdapter(this);
gv_parent.setAdapter(getAdapter()); }
private ListAdapter getAdapter() {
Log.e("set adapter", "set adapter====jss");
List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < itemTexts.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("itemText", itemTexts[i]);
list.add(map);
map.put("itemImage", itemImages[i]);
}
// 该构造函数,这里说明一下
// 第一个参数为new SimpleAdapter(Context, 上面的list,每一个项对应的itemView,itemView里显示的所有信息(要和list里的map里的名称一样) ,
// itemView里控件id);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.item_path_plan, new String[] { "itemText" ,"itemImage" },
new int[] { R.id.tv_address ,R.id.imageview });
return simpleAdapter;
}
}