今天在devp論壇裡看到有壇友問到九宮格的實現,我把我在項目中用的經驗分享一下,九宮格用gridview實現代碼。
九宮格菜單通常是全屏顯示的,那麼如何控制某個Activity全屏顯示呢,有兩種方法:
方法一:
在該Activity的onCreate函數中添加控制代碼:
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //設置Activity標題不顯示
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); //設置全屏顯示
註意:這兩句代碼必須寫在setContentView函數的前面,否則運行會報錯。
方法二:
使用XML配置文件進行配置:
(1) res/values/目錄下新增title.xml文件,用來定義onTitle的樣式,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="noTitle">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
(2) 在AndroidManifest.xml文件中,對Activity添加代碼:android:theme="@style/noTitle"
接下來開始開發九宮格的菜單
開發出來的界面截圖:
開發步驟:
(一) 放入圖標資源到res/drawble-mdpi/目錄下;
(二) 在layout下新增item.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="wrap_content">
<ImageView
android:id="@+id/ItemImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:id="@+id/ItemTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
(三) 編寫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" >
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyGridView"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center">
</GridView>
</LinearLayout>
(四) MainActivity.java文件代碼:
public class MainActivity extends Activity {
private GridView mGridView; //MyGridView
//定義圖標數組
private int[] imageRes = { R.drawable.png1, R.drawable.png2,
R.drawable.png3, R.drawable.png4, R.drawable.png5, R.drawable.png6,
R.drawable.png7, R.drawable.png8, R.drawable.png9,
R.drawable.png10, R.drawable.png11, R.drawable.png12 };
//定義標題數組
private String[] itemName = { "審前調查", "需求評估", "在冊人員", "請銷假", "集中教育",
"個別教育", "心理測評", "生活量表", "矯正方案", "矯正建議", "出勤統計", "綜合查詢" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // 設置Activity標題不顯示
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 設置全屏顯示
setContentView(R.layout.main);
mGridView = (GridView) findViewById(R.id.MyGridView);
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
int length = itemName.length;
for (int i = 0; i < length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImageView", imageRes[i]);
map.put("ItemTextView", itemName[i]);
data.add(map);
}
//為itme.xml添加適配器
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,
data, R.layout.item, new String[] { "ItemImageView","ItemTextView" }, new int[] { R.id.ItemImageView,R.id.ItemTextView });
mGridView.setAdapter(simpleAdapter);
//為mGridView添加點擊事件監聽器
mGridView.setOnItemClickListener(new GridViewItemOnClick());
}
//定義點擊事件監聽器
public class GridViewItemOnClick implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
Toast.makeText(getApplicationContext(), position + "",
Toast.LENGTH_SHORT).show();
}
}
}