GridView自动动态排版

java:

package EX04_18.txt;

import android.R.anim;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

public class EX04_18 extends Activity {
	private TextView mTextView;
	private Button mButton1, mButton2;
	private GridView mGridView;
	private String[] mGames1, mGames2;
	private ArrayAdapter<String> aryAdapter;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initApp();
        startApp();
    }

	private void startApp() {
		// TODO Auto-generated method stub
		mButton1.setOnClickListener(new Button.OnClickListener()
		{
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mGridView.setNumColumns(2);
				aryAdapter = new ArrayAdapter<String>(EX04_18.this, 
						android.R.layout.simple_list_item_1, mGames1);
				mGridView.setAdapter(aryAdapter);
				mGridView.setSelection(2);
				mGridView.refreshDrawableState();
			}
			
		});
		
		mButton2.setOnClickListener(new Button.OnClickListener()
		{
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mGridView.setNumColumns(3);
				aryAdapter = new ArrayAdapter<String>(EX04_18.this, 
						android.R.layout.simple_list_item_1, mGames2);
				mGridView.setAdapter(aryAdapter);
				mGridView.setSelection(3);
				mGridView.refreshDrawableState();
			}
			
		});
		
		mGridView.setOnItemClickListener(new GridView.OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				switch (aryAdapter.getCount()) {
				case 4:
					mTextView.setText("你选择的是:"+mGames1[arg2]);
					break;

				case 9:
					mTextView.setText("你选择的是:"+mGames2[arg2]);
					break;
				default:
					mTextView.setText("你没有导入GrideView");
					break;
				}
			}
			
		});
	}

	private void initApp() {
		// TODO Auto-generated method stub
		mTextView = (TextView)findViewById(R.id.myTextView);
		mButton1 = (Button)findViewById(R.id.myButton1);
		mButton2 = (Button)findViewById(R.id.myButton2);
		mGridView = (GridView)findViewById(R.id.myGridView);
		
		mGames1 = new String[] 
		                     {
								getResources().getString(R.string.str_list1),
								getResources().getString(R.string.str_list2),
								getResources().getString(R.string.str_list3),
								getResources().getString(R.string.str_list4)
		                     };
		mGames2 = new String[] 
		                     {
				getResources().getString(R.string.str_list1),
				getResources().getString(R.string.str_list2),
				getResources().getString(R.string.str_list3),
				getResources().getString(R.string.str_list4),
				getResources().getString(R.string.str_list1),
				getResources().getString(R.string.str_list2),
				getResources().getString(R.string.str_list3),
				getResources().getString(R.string.str_list4),
				getResources().getString(R.string.str_list1)
             };
	}
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
	android:id = "@+id/myTextView"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<GridView
	android:id = "@+id/myGridView"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />       
<Button
	android:id = "@+id/myButton1"  
	android:layout_gravity = "center"
    android:layout_width="100px" 
    android:layout_height="wrap_content" 
    android:text = "mGames1的内容"
    />
<Button
	android:id = "@+id/myButton2"  
	android:layout_gravity = "center"
    android:layout_width="100px" 
    android:layout_height="wrap_content" 
    android:text = "mGames2的内容"
    />     
</LinearLayout>

string.xml

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
    <string name="hello">Hello World, EX04_18!</string>
    <string name="app_name">EX04_18</string>
    
    <string name="str_list1">字符串1</string>
    <string name="str_list2">字符串2</string>
    <string name="str_list3">字符串3</string>
    <string name="str_list4">字符串4</string>
</resources>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值