欢迎光临
我们一直在努力

Android Coding利器之掌握小技巧,助你Coding更上一层楼~

[[198831]]

前言

话说前几天在网上浏览到一大牛写的关于Android布局优化的文章,看后感触很深,回过头看看自己写过的代码,发现还是有不少需要改进,今天找不到那天看的文章了,只能自己总结一些,以后有机会再次补充吧~ 。

本文目标

通过举例说明Android开发中的小技巧,让你我更6的玩转Android~

掌握小技巧,助你Coding更上一层楼~

本文将通过以下几点进行举例说明:

  • Android Studio 常用快捷键;
  • Android Studio 强大的代码补全;
  • Android Studio 插件;
  • Android 布局优化

Android Studio 常用快捷键

只从接触Android Studio之后,愈发离不开这个开发神器了~也是,越接触,越能感觉Studio的魅力,今天为大家带来LZ开发中常用的一些快捷键。

1. Shift+F6 全局修改 

 

 

 

LZ一般使用这种方式去改全局变量,或者资源名称。

2. Alt+F7 查看全局引用 

 

 

 

LZ一般优化时会使用这个去查看某个方法或者变量全局引用

3. Ctrl+Shift+上下键 移动代码 

 

 

 

4. 强大的 Alt+Insert 

 

 

 

Alt+Insert,这个快捷键异常强大,里面包含生成get,set,toString … 等一系列,爱不释手~

5. Ctrl+F 局部查找 

 

 

 

用这个找个方法啥的很6,但是仅限于局部,也就是本类

6. Ctrl+Alt+V 自动初始化变量 引入变量类型 

 

 

 

只适用于基本类型

7. Ctrl+D 向下复制 

 

 

 

还在Ctrl+C,Ctrl+V嘛?Out了,骚年~

8. Ctrl+Alt+L 格式化代码 

 

 

 

这个不得不说,都成为一种习惯,写好一部分总是习惯格式化下代码

9. Ctrl+Alt+O 去除无效引用(包名) 

 

 

 

去除无用引用包以及格式化代码都成为LZ标配了~GGG

10. Ctrl+J 强大的代码补全 

 

 

 

不得不说,这个真的666啊~

11. Ctrl+Alt+T 快速生成类似try块 if块 while等 

 

 

 

这个LZ一般还是用于生成try块方便,在项目中针对某些会出现异常的代码块都需要添加try

块,防止异常导致崩溃

12. 贯穿全局的Alt+Enter 

 

 

 

此子可用于导包,或者修正某些异常,类似于某些方法需要抛出异常或者添加try块等

13. Ctrl+N 查找类

 

 

 

不想用鼠标,还想找个类肿么办?Ctrl+N来帮你~

14. Ctrl+E 查看浏览历史 

 

 

 

15 .Alt+Shift+C 对比本地历史纪录 

 

 

 

这个技能不错,666

16. Ctrl+Shift+Space 自动补全代码 

 

 

 

17. Ctrl+Y 删除当前行 

 

 

 

18. F2 或Shift+F2 高亮错误或警告快速定位 

 

 

 

19. Alt+J 多行编辑 

 

 

 

想要一次性写多行么?想一次性修改多行么?完全不是梦~

关于常用快捷键介绍到此,以后用到再补充~

Android Studio 强大的代码补全

在上面已经为大家列举LZ个人开发中常用的一些快捷键,下面简单说明下强大的代码补全功能~继续感受Android Studio的魅力~

骚年,还在手动写public static final吗? 

 

 

 

作为项目中常用也几乎为必备的东西,强大的Studio早已看穿一切~当然LZ这里没有全面,大家可以自行挖掘~或者直接使用快捷键~

Android Studio 插件

强大的Android Studio背后肯定会有n多插件使其更加优秀,下面将列举LZ常用的几种,具体大家可自行发掘~

1. GsonFormat json快速生成实体类 

 

 

 

还在为Json编写实体类犯愁吗?赶快来使用它~

2. ButterKnife Zelezny 一键生成注解 简化代码 

 

 

 

使用这个需要添加相应的依赖,为了省事儿,LZ直接盗图了~勿怪

3. Parcelable 序列化 

 

 

 

Android中的序列化有两种方式,分别是实现Serializable接口和Parcelable接口,但在Android中是推荐使用Parcelable,而且这边过程比较繁琐,幸好有这神器~

4. Genymotion 优秀的模拟器

不解释,也不截图了,不过现在谷歌推出的模拟器使用起来也还不错,个人比较支持谷歌更新后的模拟器

LZ目前尝试用的也就以上几个,当然,还有很多优秀的插件,附上地址,大家自行浏览选择使用即可~

https://www.zhihu.com/question/28026027

Android 布局优化

布局优化,这是个有意思的话题,下面通过不同例子一起来看看吧~

一、巧用android:drawab系列+Style提高代码可读性以及简洁性

TextView这个东西,想必大家用的都熟透了吧?今天我们来说说如何更好玩转TextView。

举一个很通用的例子,微信”我”的界面布局 

 

 

 

大家注意到”钱包”,”收藏”,”相册”等布局了么?

以前,我们会这么写: 


 
  1. <LinearLayout 
  2.        android:layout_width="match_parent" 
  3.        android:layout_height="wrap_content" 
  4.        android:layout_marginTop="15dp" 
  5.        android:background="#FFF" 
  6.        android:orientation="horizontal" 
  7.        android:padding="15dp"
  8.  
  9.        <ImageView 
  10.            android:layout_width="30dp" 
  11.            android:layout_height="30dp" 
  12.            android:background="@mipmap/ic_launcher_1"/> 
  13.  
  14.        <TextView 
  15.            android:layout_width="match_parent" 
  16.            android:layout_height="wrap_content" 
  17.            android:layout_gravity="center_vertical" 
  18.            android:text="钱包"/> 
  19.  
  20.    </LinearLayout> 
  21.  
  22.    <LinearLayout 
  23.        android:layout_width="match_parent" 
  24.        android:layout_height="wrap_content" 
  25.        android:layout_marginTop="15dp" 
  26.        android:background="#FFF" 
  27.        android:orientation="horizontal" 
  28.        android:padding="15dp"
  29.  
  30.        <ImageView 
  31.            android:layout_width="30dp" 
  32.            android:layout_height="30dp" 
  33.            android:background="@mipmap/ic_launcher_1"/> 
  34.  
  35.        <TextView 
  36.            android:layout_width="match_parent" 
  37.            android:layout_height="wrap_content" 
  38.            android:layout_gravity="center_vertical" 
  39.            android:text="钱包"/> 
  40.  
  41.    </LinearLayout> 
  42.  
  43.    ... ...  

如果点击项多的话,反而显得乱,而且多了很多无用的代码,今天我们来尝试新的玩法~


 
  1. <TextView 
  2.        android:layout_width="match_parent" 
  3.        android:layout_height="wrap_content" 
  4.        android:layout_marginTop="15dp" 
  5.        android:background="#FFF" 
  6.        android:drawableLeft="@mipmap/ic_launcher_1" 
  7.        android:drawablePadding="10dp" 
  8.        android:gravity="center_vertical" 
  9.        android:padding="10dp" 
  10.        android:text="钱包"/> 
  11.  
  12.    <TextView 
  13.        android:layout_width="match_parent" 
  14.        android:layout_height="wrap_content" 
  15.        android:layout_marginTop="15dp" 
  16.        android:background="#FFF" 
  17.        android:drawableLeft="@mipmap/ic_launcher_1" 
  18.        android:drawablePadding="10dp" 
  19.        android:gravity="center_vertical" 
  20.        android:padding="10dp" 
  21.        android:text="钱包"/> 
  22.  
  23.    ... ...  

俩者实现效果如下: 

 

 

 TextView多的话还可以提取Style文件,代码简洁易读。

二、TextView跑马灯

附上效果图: 

 

 

 

部分代码如下: 

  1. <TextView 
  2.    android:id="@+id/id_test" 
  3.    android:layout_width="match_parent" 
  4.    android:layout_height="wrap_content" 
  5.    android:layout_marginTop="1

赞(0) 打赏
未经允许不得转载:九八云安全 » Android Coding利器之掌握小技巧,助你Coding更上一层楼~

评论 抢沙发