日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Android GridLayout使用案例詳解

瀏覽:100日期:2023-12-11 09:22:18
目錄一、簡介二、常用屬性介紹三、平分問題四、小米計算器效果五、動態加載一、簡介

GridLayout是Android4.0引入的網格布局,使用它可以減少布局嵌套。也算是常用,但一直沒仔細看過,今天研究一下

二、常用屬性介紹

GridLayout 使用屬性

屬性 作用 android:columnCount 最大列數 android:rowCount 最大行數 android:orientation GridLayout中子元素的布局方向 android:alignmentMode alignBounds:對齊子視圖邊界 alignMargins :對齊子視距內容,默認值 android:columnOrderPreserved 使列邊界顯示的順序和列索引的順序相同,默認是true android:rowOrderPreserved 使行邊界顯示的順序和行索引的順序相同,默認是true android:useDefaultMargins 沒有指定視圖的布局參數時使用默認的邊距,默認值是false

item屬性

屬性 作用 android:layout_column 指定該單元格在第幾列顯示 android:layout_row 指定該單元格在第幾行顯示 android:layout_columnSpan 指定該單元格占據的列數 android:layout_rowSpan 指定該單元格占據的行數 android:layout_gravity 指定該單元格在容器中的位置 android:layout_columnWeight (API21加入)列權重 android:layout_rowWeight (API21加入) 行權重 android:layout_gravity 作用 center 不改變元素的大小,僅居中 center_horizontal 不改變大小,水平居中 center_vertical 不改變大小,垂直居中 top 不改變大小,置于頂部 left 不改變大小,置于左邊 bottom 不改變大小,置于底部 right 不改變大小,置于右邊 start 不改變大小,根據系統語言,置于開始位置 end 不改變大小,置于結尾 fill 拉伸元素控件,填滿其應該所占的格子 fill_vertical 僅垂直方向上拉伸填充 fill_horizontal 僅水平方向上拉伸填充 clip_vertical 垂直方向上裁剪元素,僅當元素大小超過格子的空間時 clip_horizontal 水平方向上裁剪元素,僅當元素大小超過格子的空間時

注意

使用layout_columnSpan 、layout_rowSpan時要加上layout_gravity屬性,否則沒有效果;另外item在邊緣時寬高計算會出現錯誤,需要我們手動設置寬高,否則達不到想要的效果

三、平分問題

GridLayout在API21時引入了android:layout_columnWeight和android:layout_rowWeight來解決平分問題

那么在API21以前的,想要平分的話:引用兼容包

compile ’com.android.support:gridlayout-v7:25.+’

注意:

使用該控件,命名空間使用app 單獨設置app:layout_columnWeight時,這一列的所有item都設置為這個屬性,才能達到預期效果,否則這一列中設置了該屬性的item,都會被隱藏,顯示不出來 單獨設置app:layout_rowWeight時,沒有問題四、小米計算器效果

<?xml version='1.0' encoding='utf-8'?><android.support.v7.widget.GridLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android: android:layout_width='match_parent' android:layout_height='match_parent' android:background='#ece7e7' app:alignmentMode='alignBounds' app:columnCount='4' app:orientation='horizontal' app:rowCount='5' app:useDefaultMargins='false' tools:context='com.strivestay.gridlayoutdemo.MainActivity'> <!-- 如果不使用 app:layout_gravity='fill',則實際下面這個textview的寬度只是wrap_content,實現不了想要的right|bottom效果;或者,用app:layout_columnWeight='1',效果等同,填充滿 --> <TextViewandroid:gravity='right|bottom'android:text='0'app:layout_columnSpan='4'app:layout_rowWeight='3'app:layout_columnWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='AC'android:textColor='#f68904'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='退格'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='/'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='*'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='7'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='8'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='9'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='—'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='4'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='5'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='6'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='+'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='1'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='2'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='3'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#f68904'android:gravity='center'android:text='='android:textColor='#ffffff'app:layout_columnWeight='1'app:layout_rowSpan='2'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='%'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='0'app:layout_columnWeight='1'app:layout_rowWeight='1'/> <TextViewandroid:layout_margin='1dp'android:background='#ffffff'android:gravity='center'android:text='.'app:layout_columnWeight='1'app:layout_rowWeight='1'/></android.support.v7.widget.GridLayout>

效果: 4.4.4模擬器

Android GridLayout使用案例詳解

五、動態加載

1.xml引用GridLayout

<?xml version='1.0' encoding='utf-8'?><android.support.v7.widget.GridLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android: android:layout_width='match_parent' android:layout_height='match_parent' android:background='#ece7e7' app:orientation='horizontal' app:useDefaultMargins='false' app:alignmentMode='alignBounds' tools:context='com.strivestay.gridlayoutdemo.MainActivity'></android.support.v7.widget.GridLayout>

2.動態添加

package com.strivestay.gridlayoutdemo;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.GridLayout;import android.view.Gravity;import android.widget.TextView;/** * GridLayout示例 * @author StriveStay * @date 2018/3/27 */public class MainActivity extends AppCompatActivity { private String[] mStrings = {'0','AC','退格','/','*','7','8','9','—','4','5','6','+','1','2','3','=','%','0','.'}; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// xml布局//setContentView(R.layout.activity_main);// 動態添加setContentView(R.layout.activity_main2);GridLayout gridLayout = (GridLayout) findViewById(R.id.grid_layout);// 6行 4列gridLayout.setColumnCount(4);gridLayout.setRowCount(6);for (int i = 0; i < mStrings.length; i++) { TextView textView = new TextView(this); GridLayout.LayoutParams params = new GridLayout.LayoutParams(); params.width =0; params.height =0; if(i == 0){// 設置行列下標, 所占行列 ,比重// 對應: layout_row , layout_rowSpan , layout_rowWeight// 如下代表: item坐標(0,0), 占 1 行,比重為 3 ; 占 4 列,比重為 1params.rowSpec = GridLayout.spec(0,1,3f);params.columnSpec = GridLayout.spec(0,4,1f);textView.setGravity(Gravity.BOTTOM|Gravity.RIGHT); }else{// 設置行列下標,和比重params.rowSpec = GridLayout.spec((i+3)/4,1f);params.columnSpec = GridLayout.spec((i+3)%4,1f);// 背景textView.setBackgroundColor(Color.WHITE);// 字體顏色if('AC'.equals(mStrings[i])){ textView.setTextColor(Color.parseColor('#f68904'));}if('='.equals(mStrings[i])){ textView.setBackgroundColor(Color.parseColor('#f68904')); textView.setTextColor(Color.WHITE); params.rowSpec = GridLayout.spec((i+3)/4,2,1f);}// 居中顯示textView.setGravity(Gravity.CENTER);// 設置邊距params.setMargins(2,2,2,2); } // 設置文字 textView.setText(mStrings[i]); // 添加item gridLayout.addView(textView,params);} }}

效果和用xml中直接布局一樣:

Android GridLayout使用案例詳解

注意:GridLayout.spec(); 這個方法是一個重點,需要好好看一下,而且由于它有幾個重載方法,使用時也要注意。比如說下面兩個方法:

public static Spec spec(int start, int size) public static Spec spec(int start, float weight)

我剛開始就忽略了這點,本想用的是第二個帶有weight的方法,但是傳入參數時,沒有加上f,就調用了第一個方法,搞了半天才發現

所以,如果調用的是第二個方法,一定要注意float參數的表示方法,加個f,如:GridLayout.spec(0,1f);

到此這篇關于Android GridLayout使用案例詳解的文章就介紹到這了,更多相關Android GridLayout使用內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
视频一区免费在线观看| 黄页网站一区| 日本精品国产| 免费成人在线观看| 中文视频一区| 亚洲精品在线观看91| 国产精品7m凸凹视频分类| 欧美成人久久| 夜久久久久久| 中文无码久久精品| 日韩国产欧美三级| 国产精品magnet| 成人国产精品一区二区免费麻豆| 麻豆国产在线| 欧美天堂视频| 国产91精品对白在线播放| 免费视频国产一区| 香蕉国产精品| 蜜桃久久精品一区二区| 日韩av三区| 国产欧美欧美| 欧美激情国产在线| 黑丝一区二区| 欧美影院精品| 91日韩在线| 伊人成人网在线看| 日韩1区2区日韩1区2区| 久久中文精品| 婷婷精品视频| 亚洲精品乱码| 麻豆精品一区二区综合av| 国产成人久久精品一区二区三区| 成人亚洲一区二区| 国产韩日影视精品| 日韩二区三区在线观看| 国产精品二区影院| 日韩毛片视频| 亚洲欧美日韩国产| 国产情侣一区| 999国产精品永久免费视频app| 午夜一级在线看亚洲| 国产九九精品| 亚洲福利精品| 国产亚洲电影| 欧美亚洲国产一区| 欧美一区在线观看视频| 日韩欧美午夜| 亚洲日本三级| 中文av在线全新| 亚洲精品三级| 精品视频久久| 99riav1国产精品视频| 国产精品qvod| 亚洲一区二区成人| 精品国产18久久久久久二百| 免费日韩精品中文字幕视频在线| 国产精品啊啊啊| 欧美日韩国产亚洲一区| 欧美国产亚洲精品| 国产视频一区三区| 久久精品国产久精国产| 玖玖精品视频| av免费不卡国产观看| 亚洲精品乱码久久久久久蜜桃麻豆 | 婷婷综合六月| 婷婷成人av| 精品欧美一区二区三区在线观看| 日韩精品欧美大片| 亚洲男女av一区二区| 四季av一区二区凹凸精品| 日韩高清在线一区| 黄色亚洲免费| 国产精品亚洲一区二区三区在线观看| 日韩有吗在线观看| 九九久久电影| 九九99久久精品在免费线bt| 蜜桃久久久久久久| 欧美不卡在线| 福利一区视频| 国产精品高清一区二区| 在线视频亚洲欧美中文| 成人午夜国产| 麻豆国产精品777777在线| 亚洲精选av| 免费视频亚洲| 欧美羞羞视频| 精品国产一级| 国产精品网址| 91麻豆精品| 日日夜夜免费精品视频| 免费精品国产的网站免费观看| 福利精品在线| 久久99国产精品视频| 青青草国产精品亚洲专区无| 亚洲在线电影| 午夜精品亚洲| 成人久久久久| 蜜桃av.网站在线观看| 久久久久久夜| 精品黄色一级片| 国产精品一区二区中文字幕| 一级成人国产| 欧美专区在线| 91成人精品视频| 999国产精品永久免费视频app| 精品一区视频| 开心激情综合| 国产成人精品亚洲线观看 | 91精品啪在线观看国产爱臀| 在线观看一区| 亚洲一区二区日韩| 免费不卡在线视频| 视频一区二区不卡| 久久成人精品| 丝瓜av网站精品一区二区| 亚洲黄色在线| 久久午夜精品| 亚洲精品亚洲人成在线观看| 日韩欧美激情电影| 综合一区av| 亚州精品视频| 亚洲精品系列| 日韩av一区二区在线影视| 国产三级一区| 麻豆国产精品| 亚洲风情在线资源| 99视频精品视频高清免费| 欧美日韩色图| 99视频在线精品国自产拍免费观看| 99热免费精品| 亚洲人www| 欧美在线不卡| 美女视频黄免费的久久| 国产在线观看91一区二区三区| 久久久久九九精品影院| 国产精品分类| 中文字幕人成乱码在线观看| 成人免费网站www网站高清| 久久婷婷一区| 夜夜嗨网站十八久久 | 精品国产不卡一区二区| 国产精品99一区二区三| 99精品电影| 久久性天堂网| 国产精品天天看天天狠| 在线精品亚洲欧美日韩国产| 四虎影视精品| 国产婷婷精品| 欧美一区久久| 黑人精品一区| 亚洲一区中文| 国产剧情一区二区在线观看| 日韩av二区| 欧美日韩四区| 欧美天堂一区二区| 国产资源在线观看入口av| 91久久久精品国产| 日本一不卡视频| 狠狠久久伊人| 狠狠久久婷婷| 欧美久久亚洲| 国产99在线| 中文久久精品| 国产精品蜜月aⅴ在线| 久久人人97超碰国产公开结果| 99视频一区| 久久精品一区二区三区中文字幕| 国产一区久久| 国产免费av国片精品草莓男男 | 日韩一区精品| 精品国产a一区二区三区v免费| 夜鲁夜鲁夜鲁视频在线播放| 综合欧美精品| 日韩精品2区| 亚洲视频国产精品| 久久一区视频| 免费精品视频| 97精品国产一区二区三区| 鲁大师影院一区二区三区| 麻豆国产精品视频| 国产一区二区精品| 精品国产aⅴ| 免费不卡在线观看| av高清不卡| 91成人福利| 午夜久久影院| 精品高清久久| 日韩一区二区三区免费视频| 亚洲精品88| 日本视频一区二区| 久久精品国语| 国产精品对白久久久久粗| 精品91久久久久| 成人在线观看免费视频| 日本欧美在线看| 天堂√8在线中文| 国产日产精品_国产精品毛片 | 亚洲激情中文| 精品久久一区| 日韩激情综合|