Android点滴(四)

来源:互联网 发布:淘宝天天特价报名 编辑:程序博客网 时间:2024/06/10 15:15
如何为Activity屏幕的标题栏添加图标?
Java代码  收藏代码
  1. @Override    
  2. public void onCreate(Bundle icicle) {    
  3.     super.onCreate(icicle);    
  4.     Window win = getWindow();    
  5.     win.requestFeature(Window.FEATURE_LEFT_ICON);        
  6.     setContentView(R.layout.mylayout);     
  7.     win.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);    
  8. }  
要注意的是,win.setFeatureDrawableResource必须在setContentView之后,不然就没有效果。

如何让ListView自动滚动?
注意stackFromBottom以及transcriptMode这两个属性。类似Market客户端的低端不断滚动。
<ListView android:id="listCWJ"  
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent"  
     android:stackFromBottom="true"    
     android:transcriptMode="alwaysScroll"  
/>

如何设置桌面壁纸?
希望在你的程序中能设置桌面壁纸吗?很简单,首先我们需要取得设置壁纸的权限。和其它权限一样,只要在配置文件中加上以下配置信息即可。
<uses-permission android:name="android.permission.SET_WALLPAPER" />
然后在程序中调用如下代码即可设置桌面壁纸:
getApplicationContext().setWallpaper(bitmap)

如何在标题栏(titlebar)显示进度条?
Java代码  收藏代码
  1. protected void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//先给Activity注册界面进度条功能  
  4.         setContentView(R.layout.main);  
  5.         setProgressBarIndeterminateVisibility(true);//在需要显示进度条的时候调用这个方法  
  6.         setProgressBarIndeterminateVisibility(false);//在不需要显示进度条的时候调用这个方法  
  7. }  

如何去掉activity顶部的gradient?
<style name="Theme.Foo" parent="android:style/Theme.Light"> 
    <item name="android:windowContentOverlay">@null</item> 
</style>
<activity android:name=".FooActivity" 
          android:theme="@style/Theme.Foo"> ... 
http://wang-peng1.iteye.com/blog/680015

如何让ScrollView强制滑到底部?
scroll.fullScroll(View.FOCUS_DOWN) 就可以了

如何ViewFlipper去掉多余空间?
ViewFlipper flipper = (ViewFlipper)findViewById(R.id.flipper); 
flipper.setMeasureAllChildren(false); 

如何去掉tabhost横线?
Java代码  收藏代码
  1. 很简单 简单的有时候是因为我们太浮躁  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       
  3.     android:orientation="vertical"   
  4.     android:layout_width="fill_parent"   
  5.     android:layout_height="fill_parent"   
  6.    
  7.     android:gravity="center_horizontal">         
  8.    <TabHost    
  9.     android:id="@android:id/tabhost"       
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="fill_parent"   
  12.     >   
  13. ...   
  14. ...   
  15. ...   
  16.     </TabHost>   
  17.     </LinearLayout>   
  18. 外面加一层LinearLayout  

如何判断国家?
Java代码  收藏代码
  1. String locale = context.getResources().getConfiguration().locale.getCountry();    
  2. String locale = context.getResources().getConfiguration().locale.getDisplayCountry();   
  3. TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);   
  4. String countryCode = tm.getSimCountryIso();   

如何让屏幕保持一直亮?
Java代码  收藏代码
  1. @Override  
  2.     protected void onCreate(Bundle icicle) {  
  3.         super.onCreate(icicle);  
  4.   
  5.         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);  
  6.     }  

http://wang-peng1.iteye.com/blog/769561

如何检查sim卡状态?
Java代码  收藏代码
  1. TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  2.     int simState = telMgr.getSimState();  
  3.             switch (simState) {  
  4.                 case TelephonyManager.SIM_STATE_ABSENT:  
  5.                     // do something  
  6.                     break;  
  7.                 case TelephonyManager.SIM_STATE_NETWORK_LOCKED:  
  8.                     // do something  
  9.                     break;  
  10.                 case TelephonyManager.SIM_STATE_PIN_REQUIRED:  
  11.                     // do something  
  12.                     break;  
  13.                 case TelephonyManager.SIM_STATE_PUK_REQUIRED:  
  14.                     // do something  
  15.                     break;  
  16.                 case TelephonyManager.SIM_STATE_READY:  
  17.                     // do something  
  18.                     break;  
  19.                 case TelephonyManager.SIM_STATE_UNKNOWN:  
  20.                     // do something  
  21.                     break;  
  22.             }  


如何从SMS获取联系人信息?
Java代码  收藏代码
  1. ContactItem getContactByAddr(Context context, final SMSItem sms) {    
  2.     Uri personUri = Uri.withAppendedPath(    
  3.             ContactsContract.PhoneLookup.CONTENT_FILTER_URI, sms.mAddress);    
  4.     Cursor cur = context.getContentResolver().query(personUri,    
  5.             new String[] { PhoneLookup.DISPLAY_NAME },    
  6.             nullnullnull );    
  7.     if( cur.moveToFirst() ) {    
  8.         int nameIdx = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);    
  9.         ContactItem item = new ContactItem();    
  10.         item.mName = cur.getString(nameIdx);    
  11.        cur.close();    
  12.        return item;    
  13.    }    
  14.    return null;    
  15. }  

原创粉丝点击