Android中GridView通過自訂配接器實現圖文視圖排列

來源:互聯網
上載者:User

標籤:view   繼承   儲存格   視圖   etc   lis   apt   縮放   chmod   

Android中GridView組件用來以網格方式排列檢視,與矩陣類似,當螢幕上有很多元素(文字、圖片或其他元素)需要顯示時,可以使用該組件。下面我們通過代碼實現如例(為了方便,將事件處理(土司)設定到螢幕頂部)

Layout下gridview.xml布局檔案:

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" android:layout_width="match_parent" 4     android:layout_height="match_parent"> 5     <GridView 6         android:id="@+id/gv" 7         android:layout_width="match_parent" 8         android:layout_height="match_parent" 9         android:numColumns="3"10         android:columnWidth="90dp"11         android:stretchMode="columnWidth"12         android:verticalSpacing="10dp"13         android:horizontalSpacing="10dp">14     </GridView>15 </LinearLayout>

 

GridView屬性設定解析:

anroid:numColumns="3"GridView的列數設定為3

android:columnWidth="90dp",每列的寬度,也就是Item的寬度

android:stretMode="columnWidth" 縮放與列寬大小同步

android:verticalSpacing="10dp" 兩行之間的邊距

android:horizontalSpacing="10dp" 兩列之間的邊距

Layout下item_layout.xml布局檔案(即為網格內每個儲存格的布局):

 1 <?xml version="1.0" encoding="utf-8" ?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:gravity="center"(此處設定gravity為center後便不需要在他處重複設定) 6     android:orientation="vertical"> 7     <ImageView 8         android:id="@+id/iv" 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content"11         android:scaleType="centerCrop"12         android:src="@mipmap/ic_launcher" />13     <TextView14         android:id="@+id/tv"15         android:layout_width="wrap_content"16         android:layout_height="wrap_content"17         android:text="功能" (測試使用此處文字框內的文本設定可不添加)/>18 </LinearLayout>
java代碼實現功能(核心為自訂配接器):
 1 public class GridViewDemo extends AppCompatActivity { 2     private GridView gv;//聲明GridView視圖 3     @Override 4     protected void onCreate(@Nullable Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         setContentView(R.layout.gridview); 7         gv = (GridView) findViewById(R.id.gv); 8         gv.setAdapter(new MyAdapter(this));//通過設定適配器實現網格內布局 9         //為每個儲存格(item)添加單擊事件10         gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {11             @Override12             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {13                 TextView tv = (TextView) view.findViewById(R.id.tv);14                 Toast t =Toast.makeText(GridViewDemo.this,tv.getText().toString(),Toast.LENGTH_SHORT);15                 t.setGravity(Gravity.TOP,0,0);16                 t.show();17             }18         });19     }20     //自訂配接器(通過繼承BaseAdapter)21     class MyAdapter extends BaseAdapter {22         Context context;//聲明適配器中引用的上下文23         //將需要引用的圖片和文字分別封裝成數組24         int[] images = {R.mipmap.ic_launcher, R.mipmap.25                 ic_launcher, R.mipmap.ic_launcher, R.mipmap.26                 ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,};27         String[] names = {"功能1", "功能2", "功能3", "功能4", "功能5", "功能6"};28         //通過構造方法初始化上下文29         public MyAdapter(Context context) {30             this.context = context;31         }32         @Override33         public int getCount() {34             return names.length;//images也可以35         }36         @Override37         public Object getItem(int position) {38             return names[position];//images也可以39         }40         @Override41         public long getItemId(int position) {42             return position;43         }44         @Override45         public View getView(int position, View convertView, ViewGroup parent) {46             //通過布局填充器LayoutInflater填充網格儲存格內的布局47             View v = LayoutInflater.from(context).inflate(R.layout.item_layout, null);48             //使用findViewById分別找到儲存格內布局的圖片以及文字49             ImageView iv = (ImageView) v.findViewById(R.id.iv);50             TextView tv = (TextView) v.findViewById(R.id.tv);51             //引用數組內元素設定布局內圖片以及文字的內容52             iv.setImageResource(images[position]);53             tv.setText(names[position]);54             //傳回值一定為儲存格整體布局v55             return v;56         }57     }58 }

 
至此代碼全部完成,為了實現功能本次未對圖片以及文本樣式做精細化處理,感興趣的朋友們可以自己試著敲著玩玩。

 

 

Android中GridView通過自訂配接器實現圖文視圖排列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.