如何获取状态栏和标题栏的高度?1.获取状态栏高度:decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。于是,我们就可以算出状态栏的高度了。
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
2.获取标题栏高度:
getWindow().findViewById(Window.ID_ANDROID_CONTENT)
这个方法获取到的view就是程序不包括标题栏的部分,然后就可以知道标题栏的高度了。
int contentTop = getWindow()
.findViewById(Window.ID_ANDROID_CONTENT)
.getTop();
//statusBarHeight是上面所求的状态栏的高度
int titleBarHeight = contentTop - statusBarHeight;
如何将一个视窗(windows)盖在整个Application的最上面?
private ImageView waitView;
private final void showWaiting() {
try {
WindowManager.LayoutParams lp = null;
lp = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_TOAST ,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
PixelFormat.TRANSLUCENT
| WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW) ;
WindowManager mWindowManager = (WindowManager) G.appInstance
.getSystemService(Context.WINDOW_SERVICE);
if (waitView == null) {
LayoutInflater inflate = (LayoutInflater) G.appInstance
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
waitView = (ImageView) inflate.inflate(R.layout.waiting_layout,
null);
}
mWindowManager.addView(waitView, lp);
} catch (Throwable e) {
}
}
注意: 1. 要将window的类型配置成Type_toast。 2.G.appInstance 上下文需要使用Application的context. 如何判断快捷方式是否已经创建?快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中
boolean isInstallShortcut = false ;
final ContentResolver cr = context.getContentResolver();
final String AUTHORITY = "com.android.launcher.settings";
final Uri CONTENT_URI = Uri.parse("content://" +
AUTHORITY + "/favorites?notify=true");
Cursor c = cr.query(CONTENT_URI,
new String[] {"title","iconResource" },
"title=?",
new String[] {"XXX" }, null);//XXX表示应用名称。
if(c!=null && c.getCount()>0){
isInstallShortcut = true ;
}
/*try {
while (c.moveToNext()) {
String tmp = "";
tmp = c.getString(0);
}
} catch (Exception e) {
} finally {
c.close();
}*/
return isInstallShortcut ;
}
要有权限: <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>注意:2.2及其之后的版本不能用这个方法判断!(虽然在launcher.db数据库里还有favorites这个表)
网友评论