Android GridView的使用

GridView就是先將控件橫向顯示,然後縱向顯示

 

效果如上所示

先看一下GridView的佈局文件

[html]
<?xml version="1.0" encoding="utf-8"?> 
 
    <GridView 
       xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/brainheroall" 
        android:background="#FFeff1ef" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:numColumns="auto_fit"   
        android:verticalSpacing="10dp"   
        android:horizontalSpacing="10dp"   
        android:columnWidth="90dp"   
        android:stretchMode="columnWidth"   
        android:gravity="center"   
    /> 
     

<?xml version="1.0" encoding="utf-8"?>

    <GridView
       xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/brainheroall"
        android:background="#FFeff1ef"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numColumns="auto_fit" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:columnWidth="90dp" 
     android:stretchMode="columnWidth" 
     android:gravity="center" 
    />
   

這是如何顯示GridView,下面的xml文件是GridView裡面如何顯示

[html]
<?xml version="1.0" encoding="utf-8"?>   
<RelativeLayout    
         xmlns:android="http://schemas.android.com/apk/res/android"    
         android:layout_height="wrap_content"    
         android:paddingBottom="4dip" android:layout_width="fill_parent">   
         <ImageView    
               android:layout_height="wrap_content"    
               android:id="@+id/ItemImage"    
               android:layout_width="wrap_content"    
               android:layout_centerHorizontal="true">    
         </ImageView>   
         <TextView    
               android:layout_width="wrap_content"    
               android:layout_below="@+id/ItemImage"    
               android:layout_height="wrap_content"    
               android:text=""    
               android:textColor="#FF000000" 
               android:layout_centerHorizontal="true"    
               android:id="@+id/ItemText">   
         </TextView>   
</RelativeLayout>  

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout  
         xmlns:android="http://schemas.android.com/apk/res/android"  
         android:layout_height="wrap_content"  
         android:paddingBottom="4dip" android:layout_width="fill_parent"> 
         <ImageView  
               android:layout_height="wrap_content"  
               android:id="@+id/ItemImage"  
               android:layout_width="wrap_content"  
               android:layout_centerHorizontal="true">  
         </ImageView> 
         <TextView  
               android:layout_width="wrap_content"  
               android:layout_below="@+id/ItemImage"  
               android:layout_height="wrap_content"  
               android:text=""  
               android:textColor="#FF000000"
               android:layout_centerHorizontal="true"  
               android:id="@+id/ItemText"> 
         </TextView> 
</RelativeLayout>
下面是Java代碼如何使用GridView

[java]
package view; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
 
import model.HeroSimpleInfo; 
import service.ObtainInfo; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.GridView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 
 
import com.zhychneg.dota.R; 
 
public class BrainHeroActivity extends Activity implements OnItemClickListener{ 
 
    GridView gv=null; 
    long exitTime=0; 
    HeroSimpleInfo []heros=null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.brainheroall); 
        gv=(GridView) findViewById(R.id.brainheroall); 
         
         
        ArrayList<HashMap<String,Object>> al=new ArrayList<HashMap<String,Object>>(); 
     
         
        heros=ObtainInfo.getBrainHeroSimpleInfo(); 
         
         
        for(int i=0;i<heros.length;i++) 
        { 
            HashMap<String, Object> map = new HashMap<String, Object>();   
                
            map.put("icon", heros[i].getRes());//添加圖像資源的ID    
            map.put("name", heros[i].getName());//按序號做ItemText    
            al.add(map);   
        } 
         
        SimpleAdapter sa=new SimpleAdapter(this,al,R.layout.gridview_layout,new String[]{"icon","name"},new int[]{R.id.ItemImage,R.id.ItemText}); 
        gv.setAdapter(sa); 
        gv.setOnItemClickListener(this); 
         
         
         
         
         
         
    } 
     
    public void onItemClick(AdapterView<?> parent, View view, int position, 
            long id) { 
         
        Intent intent =new Intent(this,AboutHeroActivty.class); 
        intent.putExtra("HERO", heros[position]); 
        this.startActivity(intent); 
         
    } 
     
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。