《安卓開發必知的50個技巧》讀書筆記

來源:互聯網
上載者:User

標籤:android   技巧   

記錄一下看《安卓開發必知的50個技巧》覺得有用的一下技巧。

include標籤使用注意事項

如果想在標籤中覆蓋被包含布局所指定的任何android:layout_*屬性,必須在標籤中同時指定android:layout_width和android:layout_height屬性,我們可以通過指定被包含布局的layout_width和layout_height屬性都為0dp,這樣做的目的是由被包含布局的使用者在標籤中指定layout_width和layout_height屬性,如果使用者不指定這兩個屬性,它們的預設值都為0,我們也就看不到了。

ViewStub標籤
<ViewStub>    android:id="@+id/view_stub"    android:inflateId="@+id/inflateId"</ViewStub>View view=findViewById(R.id.view_stub)view.setVisibility(View.VISIBLE);

如果想獲得被填充視圖的引用,inflate()方法會直接返回該引用,可以避免再次調用findViewById()方法

自訂布局擷取自訂屬性

可以在finally中釋放TypeArray

TypeArray a=context.obtainStyledAttribute(attrs,R.styleable.Custom);try{}finally{    a.recycle();}
SpannableString
Spannable spannable=new SpannableString("");spannable.setSpan(new BackgroundColorSpan(...));
在onCreate中獲得View的寬度和高度

在onCreate裡調用getWidth()和getHeight()會返回0,可以通過View的post方法擷取寬度和高度。

發布時移除日誌語句

在混淆檔案中加入以下語句進混淆即可移除

-assumenosideeffects class android.util.Log {    public static *** d(...);}
使用委託

適配器裡刪除等操作的實現不在適配器裡實現,適配器提供一個介面,刪除操作調用介面對應的方法,其實現由Activity實現。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

《安卓開發必知的50個技巧》讀書筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.