2010/01/21

Android 利用 GridView 將 Text 呈現格子狀排列

Android 要將文字內容以格子狀排列,

可以利用 GridView 來呈現.

範例如下 :
 Java 程式碼
public class helloWorld extends Activity {

String[] vData = null;

/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// 設定 Layout 為 main.xml
setContentView( R.layout.main );

// 陣列值
vData = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i"};

// 陣列接收器
ArrayAdapter aas = new ArrayAdapter( this
, android.R.layout.simple_list_item_1
, vData
);


// 取得 GridView 物件
GridView gv = (GridView)findViewById( R.id.gridViewObj );

// 設定 GridView 的欄位數
gv.setNumColumns( 4 );

// 設定 GridView 的接收器
gv.setAdapter( aas );

// 設定 GridView 選擇時的觸發 Trigger
gv.setOnItemClickListener( new GridView.OnItemClickListener()
{
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
helloWorld.this.setTitle( "昭佑.天翔 : " + vData[arg2] );
}
});
}
}

範例結果 :