Android实现九宫格(GridView中各项平分空间)的方法

Android实现九宫格(GridView中各项平分空间)的方法,第1张

概述本文实例讲述了Android实现九宫格(GridView中各项平分空间的方法。分享给大家供大家参考。具体如下:

本文实例讲述了AndroID实现九宫格(GrIDVIEw中各项平分空间)的方法。分享给大家供大家参考。具体如下:

项目需要做一个九宫格(也不一定是9的,4宫格、16宫格、4x3宫格。。。),封了 一个宫格,它能够根据为它分配的空间来自动的调节宫中各项的尺寸。

从tableLayout集成来的,因此如果你直接在设计器上使用该封装的话需要把它自动加进去的那几个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,1.0f));         for(int x=0; x<colNum; ++x){           VIEw vIEw = adapter.getVIEw(y*colNum+x,this,row);           row.addVIEw(vIEw,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;   } }

如果你想在设计阶段就看到宫格效果的话,你可以在该空间的Tag属性上设置行列个数。比如我想看到3x3的宫格样子的话就设置成"3,3",如下图,当然你也可以在代码中使用setRowNum()和setColNum()来进行设置,但是请在设置适配器前调用这两个方法。

希望本文所述对大家的AndroID程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android实现九宫格(GridView中各项平分空间)的方法全部内容,希望文章能够帮你解决Android实现九宫格(GridView中各项平分空间)的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1142313.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存