android开发之GridLayout详解

在android4.0以上版本中,新增加了GridLayout网格布局,请参考Android 4.0开发之GridLayOut布局实践浅谈android4.0开发之GridLayout布局

    <?xml version="1.0" encoding="utf-8"?>   
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center"  
        android:columnCount="4"//该网格布局有4列  
        android:orientation="horizontal" > //水平依次排列,排满4列后,换行排列  
        <Button  
            android:layout_column="3"//该按钮定位在第4列上(从0开始计算)  
            android:text="/" />   
        <Button android:text="1" />   
        <Button android:text="2" />   
        <Button android:text="3" />   
        <Button android:text="*" />   
        <Button android:text="4" />   
        <Button android:text="5" />   
        <Button android:text="6" />   
        <Button android:text="-" />   
        <Button android:text="7" />   
        <Button android:text="8" />   
        <Button android:text="9" />   
        <Button  
            android:layout_rowSpan="3"//该按钮在一列上占3行  
            android:layout_gravity="fill"//该按钮填充占满一列3行  
            android:text="+" />   
        <Button  
            android:layout_columnSpan="2"//该按钮在一行上占2列  
            android:layout_gravity="fill"  
            android:text="0" />   
        <Button android:text="00" />   
        <Button  
            android:layout_columnSpan="3"  
            android:layout_gravity="fill"  
            android:text="=" />   
    </GridLayout>  

效果图:



在开发中有些时候完成某些功能,不得不在java代码中实现GridLayout

java代码:

    GridLayout.LayoutParams gllpTv;  
    gllpTv = new GridLayout.LayoutParams();  
    gllpTv.columnSpec = GridLayout.spec(0, 5);//相当于android:layout_columnSpan="5" spec(start,size)参数为起始位置,占几列  
    TextView timeText = new TextView(this);  
    timeText.setText("********");  
    timeText.setTextSize(26);  
    timeText.setPadding(10, 50, 0, 5);  
    gridlayout.addView(timeText, gllpTv);//把控件和布局参数添加到GridLayout  


类似Gridview的使用

    for (int j = 0; j < playRecordList.size(); j++) {  
            View v = inflater.inflate(R.layout.gridview_playrecord_item_layout, null);  
        ImageView playrecord_img = (ImageView) v.findViewById(R.id.play_record_img);  
            TextView playrecord_name = (TextView) v.findViewById(R.id.play_record_name);  
        fb.display(playrecord_img, playRecordList.get(j).getVideoImgUrl());  
        playrecord_name.setText(playRecordList.get(j).getVideoName());  
        playrecord_img.setFocusable(true);  
        playrecord_img.setClickable(true);  
        playrecord_img.setTag(playRecordList.get(j));  
        playrecord_img.setOnClickListener(myCliclListener);  
        gridlayout.addView(v);  
    }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值