欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android基礎(chǔ)之獲取LinearLayout的寬高

 更新時(shí)間:2016年11月29日 11:18:05   作者:愛開發(fā)  
LinearLayout是線性布局控件,它包含的子控件將以橫向或豎向的方式排列,按照相對(duì)位置來排列所有的widgets或者其他的containers,超過邊界時(shí),某些控件將缺失或消失。有的時(shí)候,我們需要想獲取LinearLayout寬高,下面通過這篇文章來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。

前言

看到題目獲取LinearLayout寬高,或許大家想到的解決方法如下:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  LinearLayout ll = (LinearLayout) findViewById(R.id.layInfo);
 Log.i("w", ll.getWidth()+"L"+ll.getHeight());
}

你會(huì)發(fā)現(xiàn)打印出來是0

那是因?yàn)樵趏nCreate方法的時(shí)候LinearLayout還并沒有繪制完成,所以獲取的高度均為0,

或者試著把這段代碼放到onResume()方法中去,依然是0。

實(shí)現(xiàn)方法

如果我們用獲取LinearLayout的寬高

可以通過定時(shí)器不斷的監(jiān)聽LinearLayout的寬高,等繪制完成后,關(guān)閉定時(shí)器即可。

final Handler handler= new Handler(){
   @Override
   public void handleMessage(Message msg) {
   if(msg.what == 1) {
    if(ll.getWidth()!=0) {
    Log.i("w", ll.getWidth()+"L"+ll.getHeight());
      timer.cancel();

    }
   } 
   }
  };
  timer = new Timer();
  TimerTask task = new TimerTask(){
   public void run() { 
    Message message = new Message(); 
    message.what = 1; 
    myHandler.sendMessage(message); 
    } 
   }; 
  timer.schedule(task,10,1000); 
 }

類似,如果想在Activity啟動(dòng)后立即彈出PopupWindow,我們知道在Activity的onCreate()方法中直接寫彈出PopupWindow方法會(huì)報(bào)錯(cuò),因?yàn)閍ctivity沒有完全啟動(dòng)是不能彈出PopupWindow

我們可以嘗試用兩種方法實(shí)現(xiàn):

1、用onWindowFocusChanged方法

@Override
public void onWindowFocusChanged(boolean hasFocus) {
 super.onWindowFocusChanged(hasFocus);
 showPopupWindow();
}

2、用HandlerRunnable,延時(shí)

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 mHandler.postDelayed(mRunnable, 1000);
}
private Runnable mRunnable = new Runnable() {
 public void run() {
 showPopupWindow();
 }
};

這樣獲取LinearLayout寬高問題就解決了。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開發(fā)者們能有所幫助,如果有疑問大家可以留言交流。

相關(guān)文章

最新評(píng)論