现在的位置: 首页 > 综合 > 正文

Android点滴(二)

2014年09月05日 ⁄ 综合 ⁄ 共 3408字 ⁄ 字号 评论关闭

如何获取状态栏和标题栏的高度?
1.获取状态栏高度:
decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。于是,我们就可以算出状态栏的高度了。

Java代码  收藏代码
  1. Rect frame = new Rect();  
  2. getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  
  3. int statusBarHeight = frame.top;
      

2.获取标题栏高度:
getWindow().findViewById(Window.ID_ANDROID_CONTENT)这个方法获取到的view就是程序不包括标题栏的部分,然后就可以知道标题栏的高度了。

Java代码  收藏代码
  1. int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();  
  2. //statusBarHeight是上面所求的状态栏的高度  
  3. int titleBarHeight = contentTop - statusBarHeight;
     


如何将一个视窗(windows)盖在整个Application的最上面?

Java代码  收藏代码
  1.  private ImageView waitView;  
  2. private final void showWaiting() {  
  3.  try {  
  4. WindowManager.LayoutParams lp = null;  
  5. lp = new WindowManager.LayoutParams(  
  6. ViewGroup.LayoutParams.WRAP_CONTENT,  
  7. ViewGroup.LayoutParams.WRAP_CONTENT,  
  8. WindowManager.LayoutParams.TYPE_TOAST ,  
  9. WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  
  10. | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE  
  11. | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,  
  12. PixelFormat.TRANSLUCENT  
  13. | WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW) ;  
  14. WindowManager mWindowManager = (WindowManager) G.appInstance  
  15. .getSystemService(Context.WINDOW_SERVICE);  
  16. if (waitView == null) {  
  17. LayoutInflater inflate = (LayoutInflater) G.appInstance  
  18. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  19. waitView = (ImageView) inflate.inflate(R.layout.waiting_layout,  
  20. null);  
  21. }  
  22. mWindowManager.addView(waitView, lp);  
  23. catch (Throwable e) {  
  24. }  
  25. }
     

注意: 
1. 要将window的类型配置成Type_toast。 
2.G.appInstance 上下文需要使用Application的context. 

如何判断快捷方式是否已经创建?
快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中

Java代码  收藏代码
  1.     boolean isInstallShortcut = false ;  
  2.     final ContentResolver cr = context.getContentResolver();  
  3.     final String AUTHORITY = "com.android.launcher.settings";  
  4.     final Uri CONTENT_URI = Uri.parse("content://" +  
  5.                      AUTHORITY + "/favorites?notify=true");  
  6.       
  7.     Cursor c = cr.query(CONTENT_URI,  
  8.     new String[] {"title","iconResource" },  
  9.     "title=?",  
  10.     new String[] {"XXX" }, null);//XXX表示应用名称。  
  11.             if(c!=null && c.getCount()>0){  
  12.         isInstallShortcut = true ;  
  13.     }  
  14.     /*try { 
  15.         while (c.moveToNext()) { 
  16.                                     String tmp = ""; 
  17.             tmp = c.getString(0); 
  18.         } 
  19.         } catch (Exception e) { 
  20.  
  21.         } finally { 
  22.             c.close(); 
  23.         }*/  
  24.     return isInstallShortcut ;  
  25. }
     

要有权限: 
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>

注意:2.2及其之后的版本不能用这个方法判断!(虽然在launcher.db数据库里还有favorites这个表)

如何让ListView中TextView的字体颜色跟随焦点的变化?
我们通常需要ListView中某一项选中时,他的字体颜色和原来的不一样。 如何设置字体的颜色呢? 在布局文件中TextColor一项来设置颜色,但是不是只设置一种颜色,而是在不同的条件下设置不同的颜色: 下面是个例子: 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <item android:state_enabled="false" android:color="@color/orange"></item>  
  4. <item android:state_window_focused="false" android:color="@color/orange"></item>  
  5. <item android:state_pressed="true" android:color="@color/white"></item>  
  6. <item android:state_selected="true" android:color="@color/white"></item>  
  7. <item android:color="@color/orange"></item>  
  8. </selector>   
  9. 在获取焦点或者选中的情况下设置为白色,其他情况设置为橘黄色。
     

如何在android的一个应用中调用另外一个应用?

Java代码  收藏代码
  1. Intent intent = new Intent();  
  2. //第一个参数另一个应用的包名,第二个是需要启动的类  
  3. intent.setComponent(new ComponentName("com.Ex03_03","com.Ex03_03.Ex03_03"));  
  4. startActivity(intent);
     

抱歉!评论已关闭.