成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

移動開發 Android
話說前幾天在網上瀏覽到一大牛寫的關于Android布局優化的文章,看后感觸很深,回過頭看看自己寫過的代碼,發現還是有不少需要改進,今天找不到那天看的文章了,只能自己總結一些,以后有機會再次補充吧~ 。

[[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="15dp" 
  6.    android:background="#FFF" 
  7.    android:singleLine="true" 
  8.    android:ellipsize="marquee" 
  9.    android:marqueeRepeatLimit="marquee_forever" 
  10.    android:text="失戀算個啥?輕輕的,你走吧,千萬別后悔,因為只要你一揮手,就會發現,已經有那等不及的意中人,正偷偷摸摸拉你的手!"/>  

記得Activity要設置一個屬性,否則無效。

  1. findViewById(R.id.id_test).setSelected(true); 

要想實現跑馬燈效果,必須滿足一個條件,就是TextView內容必須超出屏幕,否則無效!當然你可以自定義。

三、關于顯示價格優化

效果如下:

 

 

按照之前的想法,我們會這么玩:

  1. <TextView 
  2.      android:layout_width="wrap_content" 
  3.     android:layout_height="wrap_content" 
  4.     android:text="您需要支付:"/><TextView 
  5.     android:layout_width="wrap_content" 
  6.     android:layout_height="wrap_content" 
  7.     android:text="¥50" 
  8.     android:textColor="@android:color/holo_red_light"/>  

可有簡單用法,干嘛還多寫一個TextView,這不是浪費嘛?瞧著~

string文件中這樣寫:

  1. <string name="string_pay_price">您需要支付:%1$s</string> <!-- %1$s:代碼***位是String類型,同理,第二位%2$s --> 

Activity定義方法:

  1. private void setPayPrice(String payPrice) { 
  2.     tvShow.setText(getString(R.string.string_pay_price, payPrice)); 
  3.     SpannableStringBuilder builder = new SpannableStringBuilder(tvShow.getText().toString()); 
  4.     ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.RED); // 根據要求自定義顏色值 
  5.     builder.setSpan(colorSpan, 4, tvShow.getText().toString().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
  6.     tvShow.setText(builder); 
  7.  

四、自動鏈接TextView內容,例如網址,電話,e-mail等

效果如下: 

 

 

 

代碼部分:

  1. <TextView 
  2.     android:id="@+id/id_test" 
  3.     android:layout_width="wrap_content" 
  4.     android:autoLink="all" 
  5.     android:text="賀賀博文地址:http://blog.csdn.net/u012400885 \n查詢電話:13811604922" 
  6.     android:layout_height="wrap_content"/>  

五、巧用LinearLayout,節省不必要代碼~

下面為大家附上一個簡單效果圖,大家首先想想,如果是你,怎么寫? 

 

 

 

這里為大家引入另一種寫法,也就是小標題如何巧用LinearLayout

首先編寫shape文件,很簡單,設置高度和顏色。

  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
  3. <size android:height="15dp"/> 
  4.  
  5. <solid android:color="#FFF000"/></shape>  

其次,在布局文件中設置divider以及showDividers屬性即可。

  1. <?xml version="1.0" encoding="utf-8"?><LinearLayout 
  2.     xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     xmlns:tools="http://schemas.android.com/tools" 
  4.     android:layout_width="match_parent" 
  5.     android:layout_height="match_parent" 
  6.     android:background="#FFF" 
  7.     android:divider="@drawable/shape_0" 
  8.     android:orientation="vertical" 
  9.     android:showDividers="middle|beginning|end" 
  10.     tools:context="cn.hlq.hlqstudyandroid.MainActivity"
  11.  
  12.     <TextView 
  13.         android:layout_width="match_parent" 
  14.         android:layout_height="wrap_content" 
  15.         android:drawableLeft="@mipmap/ic_launcher_1" 
  16.         android:drawablePadding="10dp" 
  17.         android:gravity="center_vertical" 
  18.         android:padding="10dp" 
  19.         android:text="item項111"/> 
  20.  
  21.     <TextView 
  22.         android:layout_width="match_parent" 
  23.         android:layout_height="wrap_content" 
  24.         android:drawableLeft="@mipmap/ic_launcher_1" 
  25.         android:drawablePadding="10dp" 
  26.         android:gravity="center_vertical" 
  27.         android:padding="10dp" 
  28.         android:text="item項222"/></LinearLayout>  

下面簡單介紹下android:showDividers屬性:

android:showDividers="middle|beginning|end":鑒名其意,三個屬性值分別為 中間 開始部分 結束部分,也就是在三個部分顯示分割線。

有個哥兒們會說了,不是還有v7包下LinearLayoutCompat不也能實現分割線嗎?

確實,不過我試了下,感覺倆者一樣,這里就不做過分說明了~

六、禁止截屏功能,間接保護App

想想實現禁止應用截屏,只需要一行代碼,如下:

  1. getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); 

防止setContentView后即可,之后截屏的時候,會提示以下內容: 

 

 

 

結束語

東西很簡單,LZ簡單總結了下,給自己留點東西~ 

責任編輯:龐桂玉 來源: 安卓巴士Android開發者門戶
相關推薦

2023-04-26 13:55:00

Python開發技能

2023-12-19 18:08:47

MySQL方法優化查詢

2021-09-21 15:17:09

API微服務后端

2024-06-20 13:22:13

C++11C++模板

2019-08-26 14:53:32

數據中心運維管理宕機

2013-06-06 06:52:28

Ubuntu 13.0

2023-09-24 23:07:24

流量抑制風暴控制

2019-08-26 10:10:57

數據中心運維宕機

2015-03-30 09:48:33

程序員更上一層樓

2023-12-06 16:50:01

Godot 4.2開源

2021-01-21 11:24:16

智能安全首席信息安全官CISO

2011-03-31 09:51:45

Windows XP

2012-05-28 14:18:33

Web

2020-03-01 18:00:00

人工智能AI環保

2012-05-24 09:32:55

虛擬化vmareIBM

2009-10-23 14:46:43

2011-03-31 09:57:54

Windows XP

2017-11-29 15:57:00

敏捷開發DevOps

2022-03-15 10:27:40

企業CIOIT人士

2010-05-25 10:12:17

微軟虛擬化技術Hyper-V
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品电影在线观看 | 97精品超碰一区二区三区 | 伊人春色成人 | 国产精品一区二区av | 成人在线免费观看 | 99福利在线观看 | 日韩精品一区二区三区中文在线 | 黄色毛片一级 | 国产精品一区二区久久久久 | 蜜桃精品噜噜噜成人av | 一区二区三区视频在线 | 美女福利视频一区 | 看特级黄色片 | 久久久成人一区二区免费影院 | 色久影院 | 综合久久色 | 色av一区二区三区 | 精品国产免费一区二区三区演员表 | 欧美三级久久久 | 亚洲人在线播放 | 黑人精品欧美一区二区蜜桃 | 久久精品99国产精品 | 91麻豆精品国产91久久久更新资源速度超快 | 亚洲精品一区二区三区蜜桃久 | 国产精品爱久久久久久久 | 国产福利在线 | 亚洲一视频 | 亚洲国产成人精品女人久久久 | 精品国产乱码久久久久久88av | 国产精品永久免费 | 精品国产乱码久久久久久闺蜜 | 久久天堂网| 欧美一区中文字幕 | 久久国产精品免费一区二区三区 | 天天操夜夜艹 | 欧美一区二区三区在线播放 | 亚洲女人的天堂 | 日本成人福利 | 欧美成人高清 | 亚洲电影免费 | 视频第一区 |