android 编程小技巧集锦


1.android中 一个XML文件中引入另一XML的方法如下:

[html]  view plain copy
  1. <include  
  2.     android:id="@+id/inlcude"  
  3.     layout="@layout/listview_item" />  

这样做的好处可以重复利用相同的XML文件

2.android 中去除EditText的黄色边框的方法是:

  属性中添加:    
[html]  view plain copy
  1. android:background="@null"  

3.android中除去ListView滑动时候黑色背景的方法是:

属性中添加:     

[html]  view plain copy
  1. android:cacheColorHint="#00000000"  

4.android中设置没有TitleBar和全屏的方法分别是:

1.  没有TitleBa    

在AndroidManifest.xml  中Activity的theme 设置如下:

[html]  view plain copy
  1. <activity  
  2.            android:name=".Issue"  
  3.            android:theme="@android:style/Theme.NoTitleBar" /  

2.全屏

在AndroidManifest.xml  中Activity的theme 设置如下:

[html]  view plain copy
  1. <activity  
  2.             android:name=".Issue"  
  3.             android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />  

5.去除ScrollView 向上或者向下滑动时候顶部的黄边阴影


在网上搜了一些方法尝试了好多次,把我设置好的拿过来和大家分享一下

具体如下,在ScrollView中去掉滚动条的方法是

[html]  view plain copy
  1. android:scrollbars="none"  
在java中代码实现的去除黄边的方法如下  最主要的是   scrollView.setOverScrollMode(View.OVER_SCROLL_NEVER);

[java]  view plain copy
  1.        /** 
  2.  * 去除ScrollView 顶部黄边的方法,因为在android 2.2 版本以前没有滑动到顶部或者底部黄边的阴影, 
  3.  *  在2.3版本之后出现了黄色阴影, 解决办法如下, 
  4.  *  
  5.  * **/  
  6. ScrollView scrollView;  
  7.  Integer version;  
  8. scrollView = (ScrollView) findViewById(R.id.setting_scrollview);  
  9. if ((version = Integer.valueOf(android.os.Build.VERSION.SDK)) > 8) {  
  10.     Toast.makeText(Setting.this"除去黄边"1).show();  
  11.     scrollView.setOverScrollMode(View.OVER_SCROLL_NEVER);  
  12. }  

6.android中布局的一下技巧

一般控制 控件的位置、间距都需要加layout,控件中字体中的位置、间距则没

1、控件垂直居中、水平居中、居中的方法:

[html]  view plain copy
  1. android:layout_centerVertical="true"  
  2. android:layout_centerHorizontal="true"  
  3. android:layout_centerInParent="true"  


2、控件距上、下、左、右、四周的距离
[html]  view plain copy
  1. android:layout_marginTop="20dp"  
  2. android:layout_marginBottom="20dp"  
  3. android:layout_marginLeft="20dp"  
  4. android:layout_marginRight="20dp"  
  5. android:layout_margin="20dp"  


3、字体在控件中垂直居中、水平居中、居中的方法:
[html]  view plain copy
  1. android:gravity="center_vertical"  
  2. android:gravity="center_horizontal"  
  3. android:gravity="center"  


4、字体在控件中距离控件边上、下、左、右四周距离的方法
[html]  view plain copy
  1. android:paddingTop="10dp"  
  2. android:paddingBottom="10dp"  
  3. android:paddingLeft="10dp"  
  4. android:paddingRight="10dp"  
  5. android:padding="10dp"  


7.用代码实现改变组件中drawableTop、drawableBottom、drawableLeft   、 drawableRight的方法

[java]  view plain copy
  1.       checkread=(TextView)findViewById(R.id.check_read);  
  2.       checkread.setOnClickListener(new OnClickListener() {  
  3.       
  4.     @Override  
  5.     public void onClick(View v) {  
  6.         // TODO Auto-generated method stub  
  7.   
  8.         Drawable drawable= getResources().getDrawable(R.drawable.login_enter);    
  9.       /// 这一步必须要做,否则不会显示.    
  10.       drawable.setBounds(00, drawable.getMinimumWidth(), drawable.getMinimumHeight());    
  11.       checkread.setCompoundDrawables(drawable,null,null,null);    
  12.           
  13.     }  
  14. });  

8.控制软键盘不覆盖Button或则下面EditText的方法

在androidmanfest.xml 文件 activity 节点中添加   android:windowSoftInputMode="stateHidden|adjustResize" 控制键盘的隐藏和适应屏幕空间的大小。

[html]  view plain copy
  1. <activity  
  2.  android:name=".TestActivity"  
  3.  android:label="@string/app_name"  
  4.  android:windowSoftInputMode="stateHidden|adjustResize"  
  5.   >  


 9.AlertDialog 中setItems的处理

实现的效果如下:

实现的代码如下:

[java]  view plain copy
  1.                         final CharSequence[] items = { "选择相册图片""现在拍摄" };  
  2. AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  3. builder.setTitle("添加照片")  
  4. .setItems(items, new DialogInterface.OnClickListener() {  
  5.     public void onClick(DialogInterface dialog, int item) {  
  6.           
  7.         //根据设置items中数组中item的值进行监听设置  
  8.               if (item == 1){    
  9.                   Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");    
  10.                   startActivityForResult(getImageByCamera, 1);    
  11.               } else{    
  12.                   Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);    
  13.                   getImage.addCategory(Intent.CATEGORY_OPENABLE);    
  14.                   getImage.setType("image/jpeg");    
  15.                   startActivityForResult(getImage, 0);    
  16.               }       
  17.           
  18.     }  
  19. }).create().show();  

 

10.验证邮箱格式

[html]  view plain copy
  1.         //电子邮件    
  2. String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";    
  3. Pattern regex = Pattern.compile(check);    
  4. Matcher matcher = regex.matcher(<a target="_blank" href="mailto:1111111@qq.com">1111111@qq.com</a>);    
  5. boolean isMatched = matcher.matches();   


 11.android 中解决中文乱码问题

这个办法比较笨,但是比较实用,一眼就可以看出需要的是那种编码

[java]  view plain copy
  1. String temp = new String(datastr.getBytes(), "GBK");  
  2. System.out.println("****1** getBytes() -> GBK ******\n"+temp);  
  3. temp = new String(datastr.getBytes("GBK"), "UTF-8");  
  4. System.out.println("****2** GBK -> UTF-8 *******\n"+temp);  
  5. temp = new String(datastr.getBytes("GBK"), "ISO-8859-1");  
  6. System.out.println("****3** GBK -> ISO-8859-1 *******\n"+temp);  
  7. temp = new String(datastr.getBytes("ISO-8859-1"), "UTF-8");  
  8. System.out.println("****4** ISO-8859-1 -> UTF-8 *******\n"+temp);  
  9. temp = new String(datastr.getBytes("ISO-8859-1"), "GBK");  
  10. System.out.println("***5*** ISO-8859-1 -> GBK *******\n"+temp);  
  11. temp = new String(datastr.getBytes("UTF-8"), "GBK");  
  12. System.out.println("**6**** UTF-8 -> GBK *******\n"+temp);  
  13. temp = new String(datastr.getBytes("UTF-8"), "ISO-8859-1");  
  14. System.out.println("***7*** UTF-8 -> ISO-8859-1 *******\n"+temp);  

经过测试可以在这7中方法中筛选出符合自己要求的编码格式后台输出如下:


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值