1番电、一個(gè)控件在其父窗口中的坐標(biāo)位置
View.getLocationInWindow(int[] location)
2、一個(gè)控件在其整個(gè)屏幕上的坐標(biāo)位置
View.getLocationOnScreen(int[] location)
3辆琅、view.getLocationInWindow(location); //獲取在當(dāng)前窗口內(nèi)的絕對(duì)坐標(biāo)漱办;view.getLocationOnScreen(location);//獲取在整個(gè)屏幕內(nèi)的絕對(duì)坐標(biāo)。
4涎跨、getLocationInWindow洼冻,一個(gè)控件在其父窗口中的坐標(biāo)位置;getLocationOnScreen隅很,一個(gè)控件在其整個(gè)屏幕上的坐標(biāo)位置撞牢。
5、getLocationOnScreen獲取在整個(gè)屏幕內(nèi)的絕對(duì)坐標(biāo)叔营,注意這個(gè)值是要從屏幕頂端算起屋彪,也就是包括了通知欄的高度。View.getLocationInWindow()和 View.getLocationOnScreen()在window占據(jù)全部screen時(shí)绒尊,返回值相同畜挥,不同的典型情況是在Dialog中時(shí)。當(dāng)Dialog出現(xiàn)在屏幕中間時(shí)婴谱,View.getLocationOnScreen()取得的值要比View.getLocationInWindow()取得的值要大蟹但。
getLocationInWindow是以B為原點(diǎn)的C的坐標(biāo)
getLocationOnScreen以A為原點(diǎn)。
下面是getLocationOnScreen示例
start = (Button) findViewById(R.id.start);
int []location=new int[2];
start.getLocationOnScreen(location);
int x=location[0];//獲取當(dāng)前位置的橫坐標(biāo)
int y=location[1];//獲取當(dāng)前位置的縱坐標(biāo)
下面是getLocationInWindow示例
start = (Button) findViewById(R.id.start);
int []location=new int[2];
start.getLocationInWindow(location);
int x=location[0];//獲取當(dāng)前位置的橫坐標(biāo)
int y=location[1];//獲取當(dāng)前位置的縱坐標(biāo)
1.獲取狀態(tài)欄高度:
decorView是window中的最頂層view谭羔,可以從window中獲取到decorView华糖,然后decorView有個(gè)getWindowVisibleDisplayFrame方法可以獲取到程序顯示的區(qū)域,包括標(biāo)題欄瘟裸,但不包括狀態(tài)欄客叉。
于是,我們就可以算出狀態(tài)欄的高度了话告。
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
2.獲取標(biāo)題欄高度:
getWindow().findViewById(Window.ID_ANDROID_CONTENT)這個(gè)方法獲取到的view就是程序不包括標(biāo)題欄的部分兼搏,然后就可以知道標(biāo)題欄的高度了。
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
3.獲得LinearLayout布局的高和寬
View view =getLayoutInflater().inflate(R.layout.main,null);
LinearLayout linearlayout =(LinearLayout)view.findViewById(R.id.linearlayout);
//measure的參數(shù)為0即可
linearlayout.measure(0,0);
//獲取組件的寬度
int width=linearlayout.getMeasuredWidth();
//獲取組件的高度
int height=linearlayout.getMeasuredHeight();
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
或者:
View rootView = findViewById(android.R.id.content);
或者:
View rootView = findViewById(android.R.id.content).getRootView();
window沙郭、Activity佛呻、DecorView、ViewRoot關(guān)系