GridLayout 使用

GridLayout 网格布局,在开发中并不是很常用,今天偶然用了一下发现并不是太会用,所以作此笔记以避免后面再次踏坑。

GridLayout 兼容包:compile 'com.android.support:gridlayout-v7:23.+'

  • 先看布局大图:


    image.png
  • 上图是对GridLayout做了小修改后动态添加的Item。
xml布局
  • 此空间为重写GridLayout的自定义空间。
 <com.shTelecom.view.views.ActionsView
        android:id="@+id/action_msg_listLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rowCount="3"
        android:columnCount="4"
        android:orientation="horizontal"
        android:focusable="true">
    </com.shTelecom.view.views.ActionsView>
  • 要做到每个item平分GridLayout在xml布局中可以对
    android:layout_columnWeight="1"即可。
    或代码中在GridLayout 的onLayout中对View设置最小宽度为
@Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int count = getChildCount();
        if (count > lastCount +1){
            for (int i = lastCount; i < count; i++) {
                getChildAt(i).setMinimumWidth(getWidth() / getColumnCount());
                lastCount = i;
//                Log.e("onLayout" ,"-------------- lastCount: " + lastCount);
            }
        }
    }
  • 常规操作:


    image.png
<GridLayout
        android:id="@+id/action_msg_listLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rowCount="3"
        android:columnCount="4"
        android:orientation="horizontal"
        android:focusable="true">
        <Button
            android:layout_columnSpan="2"
            android:layout_columnWeight="1"
            android:text="1"/>
        <Button
            android:text="2"/>
        <Button
            android:text="3"/>
        <Button
            android:text="4"/>
        <Button
            android:text="5"/>
    </GridLayout>
View属性:
  • android:alignmentMode
    alignMargins,使视图的外边界之间进行校准。取值:
    alignBounds – 对齐子视图边界。
    alignMargins – 对齐子视距内容。
  • android:columnCount
    GridLayout的最大列数
  • android:rowCount
    GridLayout的最大行数
  • android:columnOrderPreserved
    true,使列边界显示的顺序和列索引的顺序相同。默认是true。
  • android:orientation
    GridLayout中子元素的布局方向。取值:
    horizontal – 水平布局。
    vertical – 竖直布局。
  • android:rowOrderPreserved
    true,使行边界显示的顺序和行索引的顺序相同。默认是true。
  • android:useDefaultMargins
    ture,没有指定视图的布局参数时,GridLayout使用默认的边距。默认值是false。
子View属性
  • android:layout_column
    显示该子控件的列,例如android:layout_column=”0”,表示当前子控件显示在第1列,android:layout_column=”1”,表示当前子控件显示在第2列。
  • android:layout_columnSpan
    该控件所占的列数,例如android:layout_columnSpan=”2”,表示当前子控件占两列。
  • android:layout_row
    显示该子控件的行,例如android:layout_row=”0”,表示当前子控件显示在第1行,android:layout_row=”1”,表示当前子控件显示在第2行。
  • android:layout_rowSpan
    该控件所占的列数,例如android:layout_rowSpan=”2”,表示当前子控件占两行。
  • android:layout_columnWeight (API21以上使用)
    该控件的列权重,与android:layout_weight类似,例如有GridLayout上两列,都设置android:layout_columnWeight = “1”,则两列各占GridLayout宽度的一半
  • android:layout_rowWeight (API21以上使用)
    该控件的行权重,原理同android:layout_columnWeight。
//利用代码确定View的位置  
GridLayout.LayoutParams params = new GridLayout.LayoutParams();  
for(int i=1; i <= count; ++i){  
      //首先GridLayout行列,是从0开始计算的,所以我们第一个num应该在1,0的位置  
      //设置所在列的宽度和高度  
      params.width = minWidth;   
      //设置View在表格的什么位置。Spec用来设置View在表格的位置,View占几个单元格,且View在单元格的状态(是否占满单元格的行,或者占满单元格的列)  
      params.rowSpec = GridLayout.spec(i);//行的位置。  
      params.columnSpec = GridLayout.spec(0);//列的位置。  
}
对比TableLayout,GridLayout的缺点和优点

优点:

  • 能够横向、和纵向的合并单元格,TableLayout只能够纵向合并单元格
  • 能够指定View该方法在那个格子里面,TableLayout却不能
  • GridLayout可以设置是横向添加View还是纵向添加View。TableLayout只能横向添加
    缺点:
  • 没有TableLayout 的行是行、列是列的点。
ActionsView 源码
public class ActionsView extends GridLayout implements View.OnClickListener{
    private LayoutParams layoutParams;
    private LinearLayout.LayoutParams layoutParams2;
    private LayoutInflater inflater;
    private OnClickItemListener onClickItemListener;
    private int lastCount = 0;
    private int width1 = 0;
    private String name;
    private int num;
    private int icon;

    public ActionsView(Context context) {
        super(context ,null);
    }

    public ActionsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(HORIZONTAL);
        inflater = LayoutInflater.from(getContext());
        layoutParams = new LayoutParams();
        layoutParams2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT
                ,LinearLayout.LayoutParams.WRAP_CONTENT);
        width1 = getMeasuredWidth();
    }

    public void addOnClickItemListener(OnClickItemListener onClickListener) {
        this.onClickItemListener = onClickListener;
        int size = getChildCount();
        if (onClickItemListener != null && size > 0) {
            for (int i = 0; i < size; i++) {
                getChildAt(i).setOnClickListener(this);
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int count = getChildCount();
        if (count > lastCount +1){
            for (int i = lastCount; i < count; i++) {
                getChildAt(i).setMinimumWidth(getWidth() / getColumnCount());
                lastCount = i;
//                Log.e("onLayout" ,"-------------- lastCount: " + lastCount);
            }
        }
    }

    public void addItem(String name , @DrawableRes int icon , int num) {
        this.name = name;
        this.num = num;
        this.icon = icon;
        LinearLayout layoutp = new LinearLayout(getContext());
        layoutp.setOrientation(LinearLayout.VERTICAL);
        layoutp.setGravity(Gravity.CENTER);
        layoutp.setMinimumWidth(width1 / getColumnCount());
        MsgImageView msgView = new MsgImageView(getContext());
        TextView nameView = new TextView(getContext());

        msgView.setMsgNum(num);
        msgView.setImageResource(icon);
        nameView.setText(name);
        msgView.setLayoutParams(layoutParams2);
        nameView.setLayoutParams(layoutParams2);
        layoutp.addView(msgView);
        layoutp.addView(nameView);

        layoutp.setTag(getChildCount());
        if (onClickItemListener != null) {
            layoutp.setOnClickListener(this);
        }
        addView(layoutp);
    }

    @Override
    public void onClick(View view) {
        if (this.onClickItemListener != null) {
            onClickItemListener.onItem((Integer) view.getTag());
        }
    }

    public interface OnClickItemListener {
        void onItem(int position);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,841评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,415评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,904评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,051评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,055评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,255评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,729评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,377评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,517评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,420评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,467评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,144评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,735评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,812评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,029评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,528评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,126评论 2 341

推荐阅读更多精彩内容

  • GridLayout 使用 GridLayout 是一个强大的网格布局。 基本属性 android:columnC...
    numqin阅读 1,592评论 0 0
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,295评论 25 707
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,340评论 0 17
  • 感恩今天的天气,虽然是多云,甚至是不见太阳的,但是却一点都不冷,让人倍感舒适。 感恩万科售楼部举办活动,可以一折购...
    武丹yoyo阅读 189评论 0 2
  • 本书讲述的是“道”的层面,需要反复阅读,通过时间,来真正践行! 1、以自律为前提的自由! 2、哲学:作为人,何为正...
    Eureka_Gao阅读 491评论 0 0