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

Android獲取LinearLayout的寬度和高度示例代碼

 更新時間:2023年08月19日 11:11:17   作者:田猿筆記  
這篇文章主要介紹了android獲取LinearLayout的寬度和高度,如果想直接獲取在布局文件中定義的組件的寬度和高度,可以直接使用View.getLayoutParams().width和View.getLayoutParams().height,本文結(jié)合示例代碼介紹的非常詳細,需要的朋友可以參考下

android獲取LinearLayout的寬度和高度

由于Android程序的運行機制決定了無法再組件類外部使用getWidth和getHeight方法獲得高度和寬度(在自定義組件類中可以實現(xiàn)),必須使用View.getMeasuredWidth和View.getMeasureHeight方法獲得當(dāng)前組件的寬度和高度,在調(diào)用這兩個方法之前,必須調(diào)用View.measure方法先測量組件寬度和高度。如果想直接獲取在布局文件中定義的組件的寬度和高度,可以直接使用View.getLayoutParams().width和View.getLayoutParams().height

代碼如下:

View view = getLayoutInflater().inflate(R.layout.activity_main, null);  
LinearLayout linearlayout = (LinearLayout)view.findViewById(R.id.linearlayout);  
//measure方法的參數(shù)值都設(shè)為0即可  
linearlayout.measure(0,0);  
//獲取組件寬度  
int width = linearlayout.getMeasuredWidth();  
//獲取組件高度  
int height = linearlayout.getMeasuredHeight();  

補充:Android-獲取窗口的寬度與高度

在開發(fā)的過程中,有時候需要獲取窗口的大小,在OnCreate中由于窗口還未繪制因此通過GetWidth()與GetHeight()得到的寬高都為0,那應(yīng)該如何獲取窗口的大小呢,上代碼:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){@SuppressWarnings("deprecation")@Override public void onGlobalLayout() { ? ? ? ? ? ?
    layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); ? ? ? ? ? ?int width = layout.getMeasuredWidth();int height = layout.getMeasuredHeight();} ?
});

到此這篇關(guān)于android獲取LinearLayout的寬度和高度的文章就介紹到這了,更多相關(guān)android獲取LinearLayout寬度和高度內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論