最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 獲得View寬高的幾種方式總結(jié)

 更新時(shí)間:2017年06月26日 10:19:16   作者:_小馬快跑_  
這篇文章主要介紹了Android 獲得View寬高的幾種方式總結(jié)的相關(guān)資料,需要的朋友可以參考下

《Android開發(fā)藝術(shù)探索》筆記:

在Activity的onCreate()或者onResume()中去獲得View的高度的時(shí)候不能正確獲得寬度和高度信息,這是因?yàn)?View的measure過程和Activity的生命周期不是同步執(zhí)行的,因此無法保證Activity執(zhí)行了onCreate onStart onResume時(shí),某個(gè)View已經(jīng)測(cè)量完畢了,如果還沒有測(cè)量完,那么獲得的寬高就是0??梢酝ㄟ^下面幾種方式來獲得:

1、onWindowFocusChanged

onWindowFocusChanged:View已經(jīng)初始化完畢,寬高已經(jīng)有了,需要注意onWindowFocusChanged會(huì)被調(diào)用多次,Activity得到焦點(diǎn)和失去焦點(diǎn)都會(huì)執(zhí)行這個(gè)回調(diào),見下圖:


1、Activity首次進(jìn)入的時(shí)候執(zhí)行的方法

2、跳轉(zhuǎn)到另一個(gè)Activity時(shí)

3、返回到當(dāng)前Activity時(shí)
可見當(dāng)執(zhí)行onResume和onPause時(shí),onWindowFocusChanged都會(huì)被調(diào)用。

 @Override
 public void onWindowFocusChanged(boolean hasFocus) {
   super.onWindowFocusChanged(hasFocus);
   if (hasFocus) {
     //獲得寬度
     int width = view.getMeasuredWidth();
     //獲得高度
     int height = view.getMeasuredHeight();
   }
 }

2、view.post(runnable)

通過post可以將一個(gè)runnable投遞到消息隊(duì)列的尾部,等待Looper調(diào)用此runnable的時(shí)候,View也已經(jīng)初始化好了,示例:

 @Override
 protected void onStart() {
   super.onStart();
   view.post(new Runnable() {
     @Override
     public void run() {
       int width=view.getMeasuredWidth();
       int height=view.getMeasuredHeight();
     }
   })
 }

3、ViewTreeObserver

使用ViewTreeObserver的眾多回調(diào)可以完成這個(gè)功能,比如使用OnGlobalLayoutListener這個(gè)接口,當(dāng)View樹的狀態(tài)發(fā)生改變或者View樹內(nèi)部的View的可見性發(fā)生改變時(shí),OnGlobalLayout方法將會(huì)被回調(diào),這是獲取View寬高很好的一個(gè)時(shí)機(jī),需要注意的是,伴隨著View樹的狀態(tài)改變,OnGlobalLayout會(huì)被調(diào)用多次,示例:

@Override
protected void onStart() {
  super.onStart();
  ViewTreeObserver observer=view.getViewTreeObserver();
  observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
      view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
      int width=view.getMeasuredWidth();
      int height=view.getMeasuredHeight();
    }
  });
}

4、view.measure(int widthMeasureSpec, int heightMeasureSpec)

通過手動(dòng)對(duì)View進(jìn)行measure來得到View的寬高,這里要分情況處理,根據(jù)View的LayoutParams來分:

match-parent

無法測(cè)出具體的寬高,因?yàn)楦鶕?jù)View的measure過程,構(gòu)造此種MeasureSpec需要知道parentSize,即父容器的剩余空間,而這個(gè)值我們是不知道的,所以不能測(cè)出View的大小。

具體的數(shù)值(dp/px)

比如寬高都是100px,如下measure:

 int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
 int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY);
 view.measure(widthMeasureSpec, heightMeasureSpec);

wrap_content

如下measure:

 int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec((1 << 30) - 1, View.MeasureSpec.AT_MOST);
 int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec((1 << 30) - 1, View.MeasureSpec.AT_MOST);
 view.measure(widthMeasureSpec, heightMeasureSpec);

View的specSize使用30位二進(jìn)制表示,也就是說最大是30個(gè)1,也就是(1 << 30) - 1,在最大化模式下,我們用View理論上能支持的最大值去構(gòu)造MeasureSpec是合理的。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

拉萨市| 商水县| 海林市| 建平县| 咸丰县| 北辰区| 申扎县| 仁寿县| 申扎县| 寻甸| 靖江市| 和田市| 高阳县| 长顺县| 南汇区| 嫩江县| 新河县| 长春市| 洛川县| 富民县| 邢台县| 那坡县| 玉树县| 垫江县| 昭觉县| 元江| 霍州市| 新沂市| 芜湖市| 磐石市| 金湖县| 焦作市| 绥江县| 策勒县| 桃江县| 兰坪| 道孚县| 邳州市| 仙游县| 义乌市| 宁蒗|