日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

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

發(fā)布時(shí)間:2023/11/29 Android 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Coding利器之掌握小技巧,助你Coding更上一层楼~ 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文講的是Android Coding利器之掌握小技巧,助你Coding更上一層樓~,話(huà)說(shuō)前幾天在網(wǎng)上瀏覽到一大牛寫(xiě)的關(guān)于A(yíng)ndroid布局優(yōu)化的文章,看后感觸很深,回過(guò)頭看看自己寫(xiě)過(guò)的代碼,發(fā)現(xiàn)還是有不少需要改進(jìn),今天找不到那天看的文章了,只能自己總結(jié)一些,以后有機(jī)會(huì)再次補(bǔ)充吧~ 。

本文目標(biāo)

通過(guò)舉例說(shuō)明Android開(kāi)發(fā)中的小技巧,讓你我更6的玩轉(zhuǎn)Android~

掌握小技巧,助你Coding更上一層樓~

本文將通過(guò)以下幾點(diǎn)進(jìn)行舉例說(shuō)明:

  • Android Studio 常用快捷鍵;
  • Android Studio 強(qiáng)大的代碼補(bǔ)全;
  • Android Studio 插件;
  • Android 布局優(yōu)化

Android Studio 常用快捷鍵

只從接觸Android Studio之后,愈發(fā)離不開(kāi)這個(gè)開(kāi)發(fā)神器了~也是,越接觸,越能感覺(jué)Studio的魅力,今天為大家?guī)?lái)LZ開(kāi)發(fā)中常用的一些快捷鍵。

1. Shift+F6 全局修改

LZ一般使用這種方式去改全局變量,或者資源名稱(chēng)。

2. Alt+F7 查看全局引用

LZ一般優(yōu)化時(shí)會(huì)使用這個(gè)去查看某個(gè)方法或者變量全局引用

3. Ctrl+Shift+上下鍵 移動(dòng)代碼

4. 強(qiáng)大的 Alt+Insert

Alt+Insert,這個(gè)快捷鍵異常強(qiáng)大,里面包含生成get,set,toString ... 等一系列,愛(ài)不釋手~

5. Ctrl+F 局部查找

用這個(gè)找個(gè)方法啥的很6,但是僅限于局部,也就是本類(lèi)

6. Ctrl+Alt+V 自動(dòng)初始化變量 引入變量類(lèi)型

只適用于基本類(lèi)型

7. Ctrl+D 向下復(fù)制

還在Ctrl+C,Ctrl+V嘛?Out了,騷年~

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

這個(gè)不得不說(shuō),都成為一種習(xí)慣,寫(xiě)好一部分總是習(xí)慣格式化下代碼

9. Ctrl+Alt+O 去除無(wú)效引用(包名)

去除無(wú)用引用包以及格式化代碼都成為L(zhǎng)Z標(biāo)配了~GGG

10. Ctrl+J 強(qiáng)大的代碼補(bǔ)全

不得不說(shuō),這個(gè)真的666啊~

11. Ctrl+Alt+T 快速生成類(lèi)似try塊 if塊 while等

這個(gè)LZ一般還是用于生成try塊方便,在項(xiàng)目中針對(duì)某些會(huì)出現(xiàn)異常的代碼塊都需要添加try

塊,防止異常導(dǎo)致崩潰

12. 貫穿全局的Alt+Enter

此子可用于導(dǎo)包,或者修正某些異常,類(lèi)似于某些方法需要拋出異常或者添加try塊等

13. Ctrl+N 查找類(lèi)

不想用鼠標(biāo),還想找個(gè)類(lèi)腫么辦?Ctrl+N來(lái)幫你~

14. Ctrl+E 查看瀏覽歷史

15 .Alt+Shift+C 對(duì)比本地歷史紀(jì)錄

這個(gè)技能不錯(cuò),666

16. Ctrl+Shift+Space 自動(dòng)補(bǔ)全代碼

17. Ctrl+Y 刪除當(dāng)前行

18. F2 或Shift+F2 高亮錯(cuò)誤或警告快速定位

19. Alt+J 多行編輯

想要一次性寫(xiě)多行么?想一次性修改多行么?完全不是夢(mèng)~

關(guān)于常用快捷鍵介紹到此,以后用到再補(bǔ)充~

Android Studio 強(qiáng)大的代碼補(bǔ)全

在上面已經(jīng)為大家列舉LZ個(gè)人開(kāi)發(fā)中常用的一些快捷鍵,下面簡(jiǎn)單說(shuō)明下強(qiáng)大的代碼補(bǔ)全功能~繼續(xù)感受Android Studio的魅力~

騷年,還在手動(dòng)寫(xiě)public static final嗎?

作為項(xiàng)目中常用也幾乎為必備的東西,強(qiáng)大的Studio早已看穿一切~當(dāng)然LZ這里沒(méi)有全面,大家可以自行挖掘~或者直接使用快捷鍵~

Android Studio 插件

強(qiáng)大的Android Studio背后肯定會(huì)有n多插件使其更加優(yōu)秀,下面將列舉LZ常用的幾種,具體大家可自行發(fā)掘~

1. GsonFormat json快速生成實(shí)體類(lèi)

還在為Json編寫(xiě)實(shí)體類(lèi)犯愁嗎?趕快來(lái)使用它~

2. ButterKnife Zelezny 一鍵生成注解 簡(jiǎn)化代碼

使用這個(gè)需要添加相應(yīng)的依賴(lài),為了省事兒,LZ直接盜圖了~勿怪

3. Parcelable 序列化

Android中的序列化有兩種方式,分別是實(shí)現(xiàn)Serializable接口和Parcelable接口,但在A(yíng)ndroid中是推薦使用Parcelable,而且這邊過(guò)程比較繁瑣,幸好有這神器~

4. Genymotion 優(yōu)秀的模擬器

不解釋,也不截圖了,不過(guò)現(xiàn)在谷歌推出的模擬器使用起來(lái)也還不錯(cuò),個(gè)人比較支持谷歌更新后的模擬器

LZ目前嘗試用的也就以上幾個(gè),當(dāng)然,還有很多優(yōu)秀的插件,附上地址,大家自行瀏覽選擇使用即可~

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

Android 布局優(yōu)化

布局優(yōu)化,這是個(gè)有意思的話(huà)題,下面通過(guò)不同例子一起來(lái)看看吧~

一、巧用android:drawab系列+Style提高代碼可讀性以及簡(jiǎn)潔性

TextView這個(gè)東西,想必大家用的都熟透了吧?今天我們來(lái)說(shuō)說(shuō)如何更好玩轉(zhuǎn)TextView。

舉一個(gè)很通用的例子,微信"我"的界面布局

大家注意到"錢(qián)包","收藏","相冊(cè)"等布局了么?

以前,我們會(huì)這么寫(xiě):

  • <LinearLayout?
  • ???????android:layout_width="match_parent"?
  • ???????android:layout_height="wrap_content"?
  • ???????android:layout_marginTop="15dp"?
  • ???????android:background="#FFF"?
  • ???????android:orientation="horizontal"?
  • ???????android:padding="15dp">?
  • ?
  • ???????<ImageView?
  • ???????????android:layout_width="30dp"?
  • ???????????android:layout_height="30dp"?
  • ???????????android:background="@mipmap/ic_launcher_1"/>?
  • ?
  • ???????<TextView?
  • ???????????android:layout_width="match_parent"?
  • ???????????android:layout_height="wrap_content"?
  • ???????????android:layout_gravity="center_vertical"?
  • ???????????android:text="錢(qián)包"/>?
  • ?
  • ???</LinearLayout>?
  • ?
  • ???<LinearLayout?
  • ???????android:layout_width="match_parent"?
  • ???????android:layout_height="wrap_content"?
  • ???????android:layout_marginTop="15dp"?
  • ???????android:background="#FFF"?
  • ???????android:orientation="horizontal"?
  • ???????android:padding="15dp">?
  • ?
  • ???????<ImageView?
  • ???????????android:layout_width="30dp"?
  • ???????????android:layout_height="30dp"?
  • ???????????android:background="@mipmap/ic_launcher_1"/>?
  • ?
  • ???????<TextView?
  • ???????????android:layout_width="match_parent"?
  • ???????????android:layout_height="wrap_content"?
  • ???????????android:layout_gravity="center_vertical"?
  • ???????????android:text="錢(qián)包"/>?
  • ?
  • ???</LinearLayout>?
  • ?
  • ???...?...??
  • 如果點(diǎn)擊項(xiàng)多的話(huà),反而顯得亂,而且多了很多無(wú)用的代碼,今天我們來(lái)嘗試新的玩法~

  • <TextView?
  • ???????android:layout_width="match_parent"?
  • ???????android:layout_height="wrap_content"?
  • ???????android:layout_marginTop="15dp"?
  • ???????android:background="#FFF"?
  • ???????android:drawableLeft="@mipmap/ic_launcher_1"?
  • ???????android:drawablePadding="10dp"?
  • ???????android:gravity="center_vertical"?
  • ???????android:padding="10dp"?
  • ???????android:text="錢(qián)包"/>?
  • ?
  • ???<TextView?
  • ???????android:layout_width="match_parent"?
  • ???????android:layout_height="wrap_content"?
  • ???????android:layout_marginTop="15dp"?
  • ???????android:background="#FFF"?
  • ???????android:drawableLeft="@mipmap/ic_launcher_1"?
  • ???????android:drawablePadding="10dp"?
  • ???????android:gravity="center_vertical"?
  • ???????android:padding="10dp"?
  • ???????android:text="錢(qián)包"/>?
  • ?
  • ???...?...??
  • 倆者實(shí)現(xiàn)效果如下:

    TextView多的話(huà)還可以提取Style文件,代碼簡(jiǎn)潔易讀。

    二、TextView跑馬燈

    附上效果圖:

    部分代碼如下:

  • <TextView?
  • ???android:id="@+id/id_test"?
  • ???android:layout_width="match_parent"?
  • ???android:layout_height="wrap_content"?
  • ???android:layout_marginTop="15dp"?
  • ???android:background="#FFF"?
  • ???android:singleLine="true"?
  • ???android:ellipsize="marquee"?
  • ???android:marqueeRepeatLimit="marquee_forever"?
  • ???android:text="失戀算個(gè)啥?輕輕的,你走吧,千萬(wàn)別后悔,因?yàn)橹灰阋粨]手,就會(huì)發(fā)現(xiàn),已經(jīng)有那等不及的意中人,正偷偷摸摸拉你的手!"/>??
  • 記得Activity要設(shè)置一個(gè)屬性,否則無(wú)效。

  • findViewById(R.id.id_test).setSelected(true);?
  • 要想實(shí)現(xiàn)跑馬燈效果,必須滿(mǎn)足一個(gè)條件,就是TextView內(nèi)容必須超出屏幕,否則無(wú)效!當(dāng)然你可以自定義。

    三、關(guān)于顯示價(jià)格優(yōu)化

    效果如下:

    按照之前的想法,我們會(huì)這么玩:

  • <TextView?
  • ?????android:layout_width="wrap_content"?
  • ????android:layout_height="wrap_content"?
  • ????android:text="您需要支付:"/><TextView?
  • ????android:layout_width="wrap_content"?
  • ????android:layout_height="wrap_content"?
  • ????android:text="¥50"?
  • ????android:textColor="@android:color/holo_red_light"/>??
  • 可有簡(jiǎn)單用法,干嘛還多寫(xiě)一個(gè)TextView,這不是浪費(fèi)嘛?瞧著~

    string文件中這樣寫(xiě):

  • <string?name="string_pay_price">您需要支付:%1$s</string>?<!--?%1$s:代碼第一位是String類(lèi)型,同理,第二位%2$s?-->?
  • Activity定義方法:

  • private?void?setPayPrice(String?payPrice)?{?
  • ????tvShow.setText(getString(R.string.string_pay_price,?payPrice));?
  • ????SpannableStringBuilder?builder?=?new?SpannableStringBuilder(tvShow.getText().toString());?
  • ????ForegroundColorSpan?colorSpan?=?new?ForegroundColorSpan(Color.RED);?//?根據(jù)要求自定義顏色值?
  • ????builder.setSpan(colorSpan,?4,?tvShow.getText().toString().length(),?Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);?
  • ????tvShow.setText(builder);?
  • }??
  • 四、自動(dòng)鏈接TextView內(nèi)容,例如網(wǎng)址,電話(huà),e-mail等

    效果如下:

    代碼部分:

  • <TextView?
  • ????android:id="@+id/id_test"?
  • ????android:layout_width="wrap_content"?
  • ????android:autoLink="all"?
  • ????android:text="賀賀博文地址:http://blog.csdn.net/u012400885?\n查詢(xún)電話(huà):13811604922"?
  • ????android:layout_height="wrap_content"/>??
  • 五、巧用LinearLayout,節(jié)省不必要代碼~

    下面為大家附上一個(gè)簡(jiǎn)單效果圖,大家首先想想,如果是你,怎么寫(xiě)?

    這里為大家引入另一種寫(xiě)法,也就是小標(biāo)題如何巧用LinearLayout

    首先編寫(xiě)shape文件,很簡(jiǎn)單,設(shè)置高度和顏色。

  • <shape?xmlns:android="http://schemas.android.com/apk/res/android">?
  • ?
  • <size?android:height="15dp"/>?
  • ?
  • <solid?android:color="#FFF000"/></shape>??
  • 其次,在布局文件中設(shè)置divider以及showDividers屬性即可。

  • <?xml?version="1.0"?encoding="utf-8"?><LinearLayout?
  • ????xmlns:android="http://schemas.android.com/apk/res/android"?
  • ????xmlns:tools="http://schemas.android.com/tools"?
  • ????android:layout_width="match_parent"?
  • ????android:layout_height="match_parent"?
  • ????android:background="#FFF"?
  • ????android:divider="@drawable/shape_0"?
  • ????android:orientation="vertical"?
  • ????android:showDividers="middle|beginning|end"?
  • ????tools:context="cn.hlq.hlqstudyandroid.MainActivity">?
  • ?
  • ????<TextView?
  • ????????android:layout_width="match_parent"?
  • ????????android:layout_height="wrap_content"?
  • ????????android:drawableLeft="@mipmap/ic_launcher_1"?
  • ????????android:drawablePadding="10dp"?
  • ????????android:gravity="center_vertical"?
  • ????????android:padding="10dp"?
  • ????????android:text="item項(xiàng)111"/>?
  • ?
  • ????<TextView?
  • ????????android:layout_width="match_parent"?
  • ????????android:layout_height="wrap_content"?
  • ????????android:drawableLeft="@mipmap/ic_launcher_1"?
  • ????????android:drawablePadding="10dp"?
  • ????????android:gravity="center_vertical"?
  • ????????android:padding="10dp"?
  • ????????android:text="item項(xiàng)222"/></LinearLayout>??
  • 下面簡(jiǎn)單介紹下android:showDividers屬性:

    android:showDividers="middle|beginning|end":鑒名其意,三個(gè)屬性值分別為 中間 開(kāi)始部分 結(jié)束部分,也就是在三個(gè)部分顯示分割線(xiàn)。

    有個(gè)哥兒們會(huì)說(shuō)了,不是還有v7包下LinearLayoutCompat不也能實(shí)現(xiàn)分割線(xiàn)嗎?

    確實(shí),不過(guò)我試了下,感覺(jué)倆者一樣,這里就不做過(guò)分說(shuō)明了~

    六、禁止截屏功能,間接保護(hù)App

    想想實(shí)現(xiàn)禁止應(yīng)用截屏,只需要一行代碼,如下:

  • getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);?
  • 防止setContentView后即可,之后截屏的時(shí)候,會(huì)提示以下內(nèi)容:

    結(jié)束語(yǔ)

    東西很簡(jiǎn)單,LZ簡(jiǎn)單總結(jié)了下,給自己留點(diǎn)東西~


    本文作者:佚名
    來(lái)源:51CTO
    原文標(biāo)題:Android Coding利器之掌握小技巧,助你Coding更上一層樓~

    總結(jié)

    以上是生活随笔為你收集整理的Android Coding利器之掌握小技巧,助你Coding更上一层楼~的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。