編輯:關於android開發
測試代碼:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="90dp" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:verticalSpacing="10dp" /> </RelativeLayout>
介紹一下裡面的某些屬性:
android:numColumns="auto_fit" ,GridView的列數設置為自動
android:columnWidth="90dp",每列的寬度,也就是Item的寬度
android:stretchMode="columnWidth",縮放與列寬大小同步
android:verticalSpacing="10dp",兩行之間的邊距。
android:horizontalSpacing="10dp",兩列之間的邊距。
grid_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" android:padding="10dp" > <ImageView android:id="@+id/iamgeView" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="80dp" android:layout_height="wrap_content" android:layout_marginTop="5dip" android:gravity="center" android:text="文字" android:textColor="@android:color/holo_red_light" /> </LinearLayout>
MainActivity.java:
package com.zzw.testgridview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { private GridView gridView; private List<Map<String, Object>> data; private SimpleAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); gridView = (GridView) findViewById(R.id.gridView); String[] from = { "image", "text" }; int[] to = { R.id.iamgeView, R.id.textView }; adapter = new SimpleAdapter(this, data, R.layout.grid_item, from, to); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_SHORT).show(); } }); } private void init() { // 圖片封裝成一個數組 int[] image = new int[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i }; // 文字封裝成一個數組 String[] text = new String[image.length]; for (int i = 0; i < image.length; i++) { text[i] = "文字" + i; } data = new ArrayList<Map<String, Object>>(); for (int i = 0; i < image.length; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("image", image[i]); map.put("text", text[i]); data.add(map); } } }
手機影音5--視頻播放器的基本功能(2),5--基本功能 1.設置電池狀態 1_監聽電量變化廣播 private void initData() { utils =
團隊計劃(4.20),團隊計劃4.20完成情況:了解as界面,以及基礎功能  
android 自定義圖片合集(自定義控件) 留守公司就剩下幾個人了。我沒有年假故還在堅守。廢話不多說,閒來無事。想練習一下自定義控件的應用以及學習圖片類操作以及處理
分享一個仿就看天氣應用源碼,分享天氣源碼作者xcc3641,源碼SeeWeather,就看天氣——是一款遵循Material Design風格的只