GridView的基本使用

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;
	    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值