android擷取自訂控制項位置座標,螢幕尺寸,標題列,狀態列高度,android狀態列
android擷取自訂控制項位置座標,螢幕尺寸,標題列,狀態列高度
1.擷取自訂控制項height
在本Activity中擷取當前Activity中控制項的height:
Button button = (Button)findViewById(R.id.button);int buttonHeight = button.getHeight();
在Activity中擷取其他xml檔案中控制項的height:
LayoutInflater factorys = LayoutInflater.from(當前類.this);final View textEntryView = factorys.inflate(R.layout.你要擷取的另一個XML, null);TextView y_type = (TextView) textEntryView.findViewById(R.id.控制項ID);int textHeight = y_type.getHeight();
或者
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); final View layout = inflater.inflate(R.layout.my_kebiao_setting,null); TextView y_type = (TextView)findViewById(R.id.控制項ID);int textHeight = y_type.getHeight();
2.擷取螢幕尺寸:
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int screenWidth=metrics.widthPixels; //螢幕寬度 int .screenHeight=metrics.heightPixels; //螢幕高度
這段代碼可以插入到Activity的onCreate()函數中。
3.擷取標題列、狀態列高度:
Rect rect = new Rect(); Window win = this.getWindow(); win.getDecorView().getWindowVisibleDisplayFrame(rect); int statusBarHeight = rect.top; int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop(); int titleBarHeight = contentViewTop - Variable.statusBarHeight; //statusBarHeight為狀態列高度,titleBarHeight為標題列高度
getLocationOnScreen 計算該視圖在全域座標系中的x,y值,(注意這個值是要從螢幕頂端算起,也就是包括了通知欄的高度)//擷取在當前螢幕內的絕對座標
getLocationInWindow 計算該視圖在它所在的widnow的座標x,y值,//擷取在整個視窗內的絕對座標
getLeft , getTop, getBottom, getRight, 這一組是擷取相對在它父親裡的座標
int[] location = new int[2] ;view.getLocationInWindow(location); //擷取在當前視窗內的絕對座標view.getLocationOnScreen(location);//擷取在整個螢幕內的絕對座標location [0]--->x座標,location [1]--->y座標
Android怎擷取系統高度、標題列與狀態列高度
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);///取得整個視圖部分,注意,如果你要設定標題樣式,這個必須出現在標題樣式之後,否則會出錯int top = rect.top;////狀態列的高度,所以rect.height,rect.width分別是系統的高度的寬度View v = getWindow().findViewById(Window.ID_ANDROID_CONTENT);///獲得根視圖int top2 = v.getTop();///狀態列標題列的總高度,所以標題列的高度為top2-topint width = v.getWidth();///視圖的寬度,這個寬度好像總是最大的那個int height = v.getHeight();////視圖的高度,不包括狀態列和標題列如果只想取得螢幕大小,可以用Display display = getWindowManager().getDefaultDisplay() ;
您好我想問一下怎獲得Android的標題列高度 QQ:136165000
這個問題你可以考慮風格,就是在XML裡面定義自己的style,你可以在SDK文檔中查看相關資料,Google自己的Demo中也有這樣的例子,你可以參考實現。定義好多種風格