Android實現(xiàn)九宮格(GridView中各項平分空間)的方法
本文實例講述了Android實現(xiàn)九宮格(GridView中各項平分空間)的方法。分享給大家供大家參考。具體如下:
項目需要做一個九宮格(也不一定是9的,4宮格、16宮格、4x3宮格。。。),封了 一個宮格,它能夠根據(jù)為它分配的空間來自動的調(diào)節(jié)宮中各項的尺寸。
從TableLayout集成來的,因此如果你直接在設(shè)計器上使用該封裝的話需要把它自動加進去的那幾個TableRow刪除一下。
類名為AdvancedGridView,代碼如下:
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
/**
* AdvancedGridView
* @author RobinTang
* @time 2012-10-15
*/
public class AdvancedGridView extends TableLayout {
// private static final String tag = "AdvancedGridView";
private int rowNum = 0; // row number
private int colNum = 0; // col number
private BaseAdapter adapter = null;
private Context context = null;
public AdvancedGridView(Context context) {
super(context);
initThis(context, null);
}
public AdvancedGridView(Context context, AttributeSet attrs) {
super(context, attrs);
initThis(context, attrs);
}
private void initThis(Context context, AttributeSet attrs) {
this.context = context;
if (this.getTag() != null) {
String atb = (String) this.getTag();
int ix = atb.indexOf(',');
if (ix > 0) {
rowNum = Integer.parseInt(atb.substring(0, ix));
colNum = Integer.parseInt(atb.substring(ix+1, atb.length()));
}
}
if (rowNum <= 0)
rowNum = 3;
if (colNum <= 0)
colNum = 3;
if(this.isInEditMode()){
this.removeAllViews();
for(int y=0; y<rowNum; ++y){
TableRow row = new TableRow(context);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f));
for(int x=0; x<colNum; ++x){
View button = new Button(context);
row.addView(button, new TableRow.LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
}
this.addView(row);
}
}
}
public BaseAdapter getAdapter() {
return adapter;
}
public void setAdapter(BaseAdapter adapter) {
if(adapter != null){
if(adapter.getCount() < this.rowNum*this.colNum){
throw new IllegalArgumentException("The view count of adapter is less than this gridview's items");
}
this.removeAllViews();
for(int y=0; y<rowNum; ++y){
TableRow row = new TableRow(context);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f));
for(int x=0; x<colNum; ++x){
View view = adapter.getView(y*colNum+x, this, row);
row.addView(view, new TableRow.LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
}
this.addView(row);
}
}
this.adapter = adapter;
}
public int getRowNum() {
return rowNum;
}
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
public int getColNum() {
return colNum;
}
public void setColNum(int colNum) {
this.colNum = colNum;
}
}
如果你想在設(shè)計階段就看到宮格效果的話,你可以在該空間的Tag屬性上設(shè)置行列個數(shù)。比如我想看到3x3的宮格樣子的話就設(shè)置成"3,3",如下圖,當(dāng)然你也可以在代碼中使用setRowNum()和setColNum()來進行設(shè)置,但是請在設(shè)置適配器前調(diào)用這兩個方法。

希望本文所述對大家的Android程序設(shè)計有所幫助。
相關(guān)文章
詳解Android應(yīng)用中preference首選項的編寫方法
這篇文章主要介紹了Android應(yīng)用中preference首選項的編寫方法,或許Apple將其翻譯為'偏好設(shè)置'更直觀些,即用戶對應(yīng)用的一些個性化調(diào)整菜單,需要的朋友可以參考下2016-04-04
Android AIDL實現(xiàn)與服務(wù)相互調(diào)用方式
這篇文章主要介紹了Android AIDL實現(xiàn)與服務(wù)相互調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
SurfaceView開發(fā)[捉小豬]手機游戲 (一)
這篇文章主要介紹了用SurfaceView開發(fā)[捉小豬]手機游戲 (一)本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
Android開發(fā)中如何去掉app標題欄的實現(xiàn)
這篇文章主要介紹了Android開發(fā)中如何去掉app標題欄的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Android 實現(xiàn)旋轉(zhuǎn)木馬的音樂效果
大家一定在百度音樂上在線聽過歌,有沒有注意到那個旋轉(zhuǎn)的唱片,本篇文章主要介紹在Android上如何實現(xiàn)這樣的功能,有需要的小伙伴可以參考下2016-07-07
Android動態(tài)添加設(shè)置布局與控件的方法
這篇文章主要介紹了Android動態(tài)添加設(shè)置布局與控件的方法,涉及Android中布局與控件的相關(guān)操作技巧,需要的朋友可以參考下2016-01-01

