bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Android中怎么使用ViewGroup自定義布局

本篇文章為大家展示了Android中怎么使用ViewGroup自定義布局,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括寧鄉(xiāng)網(wǎng)站建設(shè)、寧鄉(xiāng)網(wǎng)站制作、寧鄉(xiāng)網(wǎng)頁制作以及寧鄉(xiāng)網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,寧鄉(xiāng)網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到寧鄉(xiāng)省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

步驟

這里 我為大家設(shè)計(jì)一個(gè) 類似 LinearLayout 線性布局的 ViewGroup 作為范例。

首先,如果是一個(gè) LinearLayout 那么當(dāng)設(shè)置 wrap_content 時(shí),他就會(huì)以子空間中最寬的那個(gè)為它的寬度。同時(shí)在高度方面會(huì)是所有子控件高度的總和。所以我們先寫兩個(gè)方法,分別用于測(cè)量 ViewGroup 的寬度和高度。

 private int getMaxWidth(){
  int count = getChildCount();
  int maxWidth = 0;
  for (int i = 0 ; i < count ; i ++){
   int currentWidth = getChildAt(i).getMeasuredWidth();
   if (maxWidth < currentWidth){
    maxWidth = currentWidth;
   }
  }
  return maxWidth;
 }
 
 private int getTotalHeight(){
  int count = getChildCount();
  int totalHeight = 0;
  for (int i = 0 ; i < count ; i++){
   totalHeight += getChildAt(i).getMeasuredHeight();
  }
  return totalHeight;
 }

對(duì)于 ViewGroup 而言我們可以粗略的分為兩種模式:固定長寬模式(match_parent),自適應(yīng)模式(wrap_content),根據(jù)這兩種模式,就可以對(duì) ViewGroup 的繪制進(jìn)行劃分。這里關(guān)于 measureChildren 這個(gè)方法,他是用于將所有的子 View 進(jìn)行測(cè)量,這會(huì)觸發(fā)每個(gè)子 View 的 onMeasure 函數(shù),但是大家要注意要與 measureChild 區(qū)分,measureChild 是對(duì)單個(gè) view 進(jìn)行測(cè)量

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
  measureChildren(widthMeasureSpec, heightMeasureSpec);
 
  int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  int width  = MeasureSpec.getSize(widthMeasureSpec);
  int heightMode= MeasureSpec.getMode(heightMeasureSpec);
  int height = MeasureSpec.getSize(heightMeasureSpec);
 
  if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST){
   int groupWidth = getMaxWidth();
   int groupHeight= getTotalHeight();
 
   setMeasuredDimension(groupWidth, groupHeight);
  }else if (widthMode == MeasureSpec.AT_MOST){
   setMeasuredDimension(getMaxWidth(), height);
  }else if (heightMode == MeasureSpec.AT_MOST){
   setMeasuredDimension(width, getTotalHeight());
  }
 }

重寫 onLayout

整完上面這些東西,我們的布局大小七十九已經(jīng)出來了,然我們?cè)诨顒?dòng)的布局文件里面加上它,并添加上幾個(gè)子 View 然后運(yùn)行一下,先看看效果:

 <com.entry.android_view_user_defined_first.views.MyLinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/colorAccent">
 
  <Button
   android:layout_width="100dp"
   android:layout_height="50dp"
   android:text="qwe"/>
 
  <Button
   android:layout_width="250dp"
   android:layout_height="150dp"
   android:text="qwe"/>
 
  <Button
   android:layout_width="200dp"
   android:layout_height="75dp"
   android:text="qwe"/>
 
 </com.entry.android_view_user_defined_first.views.MyLinearLayout>

運(yùn)行效果如下:

Android中怎么使用ViewGroup自定義布局

我們看見布局出來了,大小好像也沒啥問題,但是子 View 呢??! 這么沒看見子 View 在看看代碼,系統(tǒng)之前然我們重寫的 onLayout() 還是空著的呀!!也就是說,子 View 的大小和位置根本就還沒有進(jìn)行過設(shè)定!讓我們來重寫下 onLayout() 方法。

 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  int count = getChildCount();
  int currentHeight = 0;
  for (int i = 0 ; i < count ; i++){
   View view = getChildAt(i);
   int height = view.getMeasuredHeight();
   int width = view.getMeasuredWidth();
   view.layout(l, currentHeight, l + width, currentHeight + height);
   currentHeight += height;
  }
 }

上述內(nèi)容就是Android中怎么使用ViewGroup自定義布局,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站欄目:Android中怎么使用ViewGroup自定義布局
本文路徑:http://vcdvsql.cn/article48/pejphp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)云服務(wù)器商城網(wǎng)站搜索引擎優(yōu)化建站公司定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)