Android中GridView和ArrayAdapter用法執行個體分析_Android

來源:互聯網
上載者:User

本文執行個體分析了Android中GridView和ArrayAdapter用法。分享給大家供大家參考,具體如下:

GridView是一個表格化的二維排版的View,當GridView的文字放不下時會出現scrolling的效果,GridView中的元素命名為Item,要將Item放入GridView,需要ArrayAdapter對象。

例子如下:

import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.GridView;import android.widget.TextView;public class A06Activity extends Activity { private TextView tv;//兩個按鈕是動態放入GridView的開關,b01是設定GridView為兩列格式,並放入4//個item,b02設定為三列格式,並放入9個Item private Button b01,b02;// private GridView gv; private String[] s1,s2; private ArrayAdapter<String> aa;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    tv=(TextView)findViewById(R.id.tv);    b01=(Button)findViewById(R.id.button01);    b02=(Button)findViewById(R.id.button02);    gv=(GridView)findViewById(R.id.gv);    s1=new String[]{     getResources().getString(R.string.str_list01),     getResources().getString(R.string.str_list02),     getResources().getString(R.string.str_list03),     getResources().getString(R.string.str_list04)    };    s2=new String[]{     getResources().getString(R.string.str_list01),     getResources().getString(R.string.str_list02),     getResources().getString(R.string.str_list03),     getResources().getString(R.string.str_list04),     getResources().getString(R.string.str_list01),     getResources().getString(R.string.str_list02),     getResources().getString(R.string.str_list03),     getResources().getString(R.string.str_list04),     getResources().getString(R.string.str_list04)    };    b01.setOnClickListener(new OnClickListener(){  @Override  public void onClick(View v) {  // TODO Auto-generated method stub  gv.setNumColumns(2);  aa=new ArrayAdapter<String>(A06Activity.this,R.layout.list_item_l_small,s1);  gv.setAdapter(aa);  gv.setSelection(2);  gv.refreshDrawableState();  }  });  b02.setOnClickListener(new OnClickListener(){  @Override  public void onClick(View v) {  // TODO Auto-generated method stub  gv.setNumColumns(2);  aa=new ArrayAdapter<String>(A06Activity.this,R.layout.list_item_l_small,s2);  gv.setAdapter(aa);  }  });  //利用GridView的OnClickListener()方法用來捕捉使用者單擊的是哪個item  gv.setOnItemClickListener(new OnItemClickListener(){  @Override  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,   long arg3) {  // 判斷Adapter中元素的個數,判斷被單擊的是第幾個元素的名稱  switch(aa.getCount()){  case 4:   tv.setText(s1[arg2]);   break;  case 9:   tv.setText(s2[arg2]);   break;  }  }  });  }}

res/layout/main.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" >  <TextView    android:id="@+id/tv"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/hello" />  <Button     android:id="@+id/button01"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    />  <Button     android:id="@+id/button02"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    />  <GridView     android:id="@+id/gv"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    ></GridView></LinearLayout>

res/layout/list_item_l_small的作用是配置ArrayAdapter的第二個參數textViewResourceId,參數必須為TextView進行配置。若需要更改放入item的文字模式,可以通過這個來設定文字的大小、模式的屬性設定,而一旦放入ArrayAdapter,這個配置會立即生效。也可以設定許多的layout xml,提供給不同的GridView使用,讓每一個GridView都有不同的風貌。

<?xml version="1.0" encoding="utf-8"?><TextView   xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/myCheckedTextView1"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:textColor="@drawable/blue"  android:textSize="12dp"  android:textAppearance="@android:attr/textAppearanceLarge"  android:gravity="center_vertical"  android:paddingLeft="6dip"  android:minHeight="@android:attr/listPreferredItemHeight"/>

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android基本組件用法總結》及《Android控制項用法總結》

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.