安卓开发gridview根据屏幕宽度行列相同的单元格集合

familynt 2015-04-06

前不久网上有人问如何在java代码里根据屏幕宽度动态设定每一行的单元格数。百度了好久,没结果,结果自己认真分析,实现了这个功能。

gridview可以在xml布局里面设定每一个单元的宽度,以及手机屏幕的宽度来确定,一行能够容纳的单元格数。怎样在java代码里,动态设定一行的单元格数呢?

package com.lin.test;

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

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class SecondActivity extends Activity{
	GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	init();
	 List<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
     for(int i=0;i<MyProject001Activity.size;i++){
     	HashMap<String, Object> mp= new HashMap<String,Object>();
     	mp.put("image", R.drawable.ic_launcher);
     	mp.put("text","第"+(i+1)+"关");
     	list.add(mp);
     }
     SimpleAdapter simpleAdapter=new SimpleAdapter(getApplicationContext(), list, R.layout.item, new String[]{
     	"image","text"
     }, new int[]{
     	R.id.imageView,R.id.tv
     });
     gridView.setAdapter(simpleAdapter);
     gridView.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Toast.makeText(getApplicationContext(), arg2, 3).show();
		}
   	  
     });
}
private void init() {
	// TODO Auto-generated method stub
	gridView=(GridView) findViewById(R.id.GridView);
	WindowManager wm = this.getWindowManager();//获取WindowManager
    int width = wm.getDefaultDisplay().getWidth();//获取到屏幕宽度
     double width2=width/Math.sqrt(MyProject001Activity.size);//用屏幕的宽度比上单元格的个数,就是每一个单元格的宽度
    System.out.println("width"+width);
    System.out.println("width2"+width2);
	gridView.setColumnWidth((int) width2);//gridview动态设置每一个单元格的宽度
	}
}


 

second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    
<GridView  
     android:id="@+id/GridView"   
     android:layout_width="fill_parent"   
     android:layout_height="fill_parent"       
     android:numColumns="auto_fit"    
      android:stretchMode="columnWidth"   
     android:gravity="center">   
</GridView>
</LinearLayout>

MyProject001Activity.java

package com.lin.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MyProject001Activity extends Activity {
    /** Called when the activity is first created. */
	EditText tv;
	public static int size=0;
	//定义静态变量,作为设定的每一行的单元格数。
	
	//其实可以通过界面跳转intent传值,这里我简化了
	Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
       
     button.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			final int bianchang=  Integer.parseInt(tv.getText().toString());
		       size=bianchang*bianchang;
		       startActivity(new Intent(MyProject001Activity.this,SecondActivity.class));
		}
	});
    }
	private void init() {
		// TODO Auto-generated method stub
	tv=	(EditText) findViewById(R.id.tv);
	button=	(Button) findViewById(R.id.button);
		
	}
}

main.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:orientation="vertical" >
<EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:hint="正方形的边长?"
         />
    <Button 
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="提交"
        />

</LinearLayout>
item.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:orientation="vertical">
<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_gravity="center"
    />
 <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:layout_gravity="center"
        />

</LinearLayout>

效果图:

在界面A,输入5,点击提交
安卓开发gridview根据屏幕宽度行列相同的单元格集合

在界面B保证长行列的单元格数相同
 
安卓开发gridview根据屏幕宽度行列相同的单元格集合

最后别忘了清单文件

        <activity android:name=".SecondActivity"/>

相关推荐

sgafdsg / 0评论 2011-01-24