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

android有用代码片段2

2013年10月07日 ⁄ 综合 ⁄ 共 2435字 ⁄ 字号 评论关闭

二十一、获取手机屏幕分辨率

[java] view
plain
copy

  1. DisplayMetrics  dm = new DisplayMereics();  
  2.   
  3.         getWindowManager().getDefaultDisplay().getMetrics(dm);  
  4.   
  5.         float width = dm.widthPixels * dm.density;  
  6.   
  7.         float height = dm.heightPixels * dm.density  
    在这里问什么要乘以  dm.density   了,是因为通过dm.widthPixels的到的结果始终是320,不是真实的屏幕分辨率,所以要乘以dm.density得到真实的分辨率。

     二十二、在Activity里面播放背景音乐

[java] view
plain
copy

  1. public void onCreate(Bundle savedInstanceState) {  
  2.              super.onCreate(savedInstanceState);  
  3.              setContentView(R.layout.mainlay);  
  4.              mediaPlayer = MediaPlayer.create(this, R.raw.mu);  
  5.              mediaPlayer.setLooping(true);  
  6.              mediaPlayer.start();  
  7.   
  8.                    }  

      二十三、让程序的界面不随机器的重力感应而翻转

                 第一种方法,在manifast文件里面

[html] view
plain
copy

  1. <activity  
  2.   android:screenOrientation="portrait">  
  3.   </activity>  


                 第二种,在代码里面

[java] view
plain
copy

  1. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  



    二十四、使activity全屏显示

[java] view
plain
copy

  1. requestWindowFeature(Window.FEATURE_NO_TITLE);  
  2.         getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,        
  3.                 WindowManager.LayoutParams. FLAG_FULLSCREEN);
     

        二十五、在RelativeLayout中使selector要注意点

         关于selector的使用方法,可以参考http://blog.csdn.net/aomandeshangxiao/article/details/6759576这篇文章,今天,遇到在RelativeLayout中添加background为selector后没有反应的问题,寻摸了很长时间,一直没有找到原因,其实只要加上一句代码就完全可以解决:

[java] view
plain
copy

  1. <span style="font-size:16px;">RelativeLayout 里面加上android:clickable="true"</span>  


这样,RelativLayout就会出现在selector里面定义的效果。


   二十六、显示或隐藏虚拟键盘

[java] view
plain
copy

  1. 显示:  
  2. InputMethodManager imm = (InputMethodManager)(getSystemService(Context.INPUT_METHOD_SERVICE));  
  3. imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);  
  4.   
  5. 隐藏:  
  6. InputMethodManager imm = (InputMethodManager)(getSystemService(Context.INPUT_METHOD_SERVICE));  
  7. imm.hideSoftInputFromWindow(m_edit.getWindowToken(), 0);  



   二十七、退出程序时清除通知中信息  

[java] view
plain
copy

  1. NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  2. nm.cancelAll();  



     二十八、创建快捷方式

[java] view
plain
copy

  1. Intent intent=new Intent();  
  2. //设置快捷方式的图标  
  3. intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.img));  
  4. //设置快捷方法的名称  
  5. intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "点击启动哥的程序");            //设置点击快键图标的响应操作  
[java] view
plain
copy

  1. intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this,MainActivity.class));  

抱歉!评论已关闭.