进阶之代码应用技巧

博客出自: http://blog.csdn .NET /liuxian13183 ,转载注明出处! All Rights Reserved ! 

1、给文字加下划线
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">email_name.setText(Html.fromHtml("<u>" + email.title + "</u>"));</span>  
2、文字图片一起排,并加以间隔
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">        android:drawableLeft="@drawable/user_id"  
  2.         android:drawablePadding="10dip"</span>  

3、获得手机串号
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">TelephonyManager telephonyManager = (TelephonyManager) MarketApplication.sContext  
  2. .getSystemService(Context.TELEPHONY_SERVICE);   
  3. String imei = telephonyManager.getDeviceId(); </span>  

4、改变tabwidget背景色
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">      tabWidget.getChildAt(i).setBackgroundResource(  
  2.                     R.drawable.tab_bar_mask3);</span>  

5、去掉tabhost下的白线
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">  tabHost.setPadding(tabHost.getPaddingLeft(), tabHost.getPaddingTop(),  
  2.                 tabHost.getPaddingRight(), tabHost.getPaddingBottom() - 10);</span>  
6、获得View在控件的上下左右间距
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">      int[] loc = new int[2];  
  2.         // if (fromWindowsLoc) {  
  3.         // anchor.getLocationInWindow(loc);  
  4.         // } else {  
  5.         anchor.getLocationOnScreen(loc);  
  6.         // // }  
  7.         int l = loc[0];//左  
  8.         int t = loc[1];//上  
  9.         // 组件上下:445:474  
  10.         int r = l + anchor.getWidth();// 右  
  11.         int b = t + anchor.getHeight();// 下  
  12.         Rect localRect = new Rect(l, t, r, b);</span>  
7、获得sd卡上的图片,并加载
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">ImageView.setImageUri(Uri.fromFile(new File("/sdcard/cats.jpg"))); </span>  

8、解析文字
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">URLDecoder.decode(searchkeyword, "gb2312")</span>  

9、Activity里嵌入Activity
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">      ll.removeAllViews();  
  2.                 Intent it = new Intent(new Intent(SearchActivity.this,  
  3.                         SearchAndroidList.class));  
  4.                 it.putExtra(AppData.PAPER_QUERY, query);  
  5.                 ll.addView(getLocalActivityManager().startActivity(  
  6.                         "SearchAndroidList",  
  7.                         it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
  8.                         .getDecorView());</span>  

10、 Android  屏幕常亮保持
相关的变量:
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;"> PowerManager powerManager = null;  
  2.     WakeLock wakeLock = null;</span>  

初始化:
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">powerManager = (PowerManager) this  
  2.                 .getSystemService(Service.POWER_SERVICE);  
  3.         wakeLock = this.powerManager.newWakeLock(  
  4.                 PowerManager.SCREEN_DIM_WAKE_LOCK, "My Lock");</span>  

开启屏幕常亮:
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;"> wakeLock.setReferenceCounted(false);  
  2.             wakeLock.acquire();</span>  

关闭你的程序设置的屏幕常亮:
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;"> wakeLock.release();</span>  

注意的地方:我在实际的调试过程中,没有设置wakeLock.setReferenceCounted(false);这个,报错为:java.lang.RuntimeException: WakeLock under-locked


                 阅读源码:
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">acquire()函数如下:  
  2.   
  3.                         public void acquire()  
  4.         {  
  5.             synchronized (mToken) {  
  6.                 if (!mRefCounted || mCount++ == 0) {  
  7.                     try {  
  8.                         mService.acquireWakeLock(mFlags, mToken, mTag);  
  9.                     } catch (RemoteException e) {  
  10.                     }  
  11.                     mHeld = true;  
  12.                 }  
  13.             }  
  14.         }  
  15.   
  16.   release()函数如下:  
  17.   
  18.                public void release()  
  19.         {  
  20.             release(0);  
  21.         }  
  22.   
  23.        public void release(int flags)  
  24.         {  
  25.             synchronized (mToken) {  
  26.                 if (!mRefCounted || --mCount == 0) {  
  27.                     try {  
  28.                         mService.releaseWakeLock(mToken, flags);  
  29.                     } catch (RemoteException e) {  
  30.                     }  
  31.                     mHeld = false;  
  32.                 }  
  33.                 if (mCount < 0) {  
  34.                     throw new RuntimeException("WakeLock under-locked " + mTag);  
  35.                 }  
  36.             }  
  37.         }</span>  

报错就抱在release(int flags)中,mCount为负数了,抛除了异常


我们再看下:setReferenceCounted(boolean flags);
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">   public void setReferenceCounted(boolean value)  
  2.         {  
  3.             mRefCounted = value;  
  4.         }  
  5. </span>  

这个函数的作用是是不是需要计算锁的数量,设置为false时,在release()的时候,不管你acquire()了多少回,可以releaseWakeLock掉


11、不让图片变形充满整个屏幕
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">  imageView.setAdjustViewBounds(true);//不让图片充满全局</span>  
12、设背景色为透明
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">getWindow().setBackgroundDrawable(new ColorDrawable(0));</span>  

13、中文排版不整齐,一个match_parent解决问题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值