android 開發Tips - sdk api

收集網上看到,或者自己總結的tips

  1. view.isShown () 當view本身以及它的所有祖先們都是visible時,isShown()才返回TRUE。
    而平常我們調用if(view.getVisibility() == View.VISIBLE)只是對view本身而不對祖先的可見性進行判斷

  2. android.text.format.Formatter 方法 :public static String formatFileSize(Context context, long number)
    number 的單位是B,返回如:3.33MB (B->KB->MB->GB->TB->PB)

<!-- Suffix added to a number to signify size in bytes. -->
<string name="byteShort">B</string>
<!-- Suffix added to a number to signify size in kilobytes. -->
<string name="kilobyteShort">KB</string>
<!-- Suffix added to a number to signify size in megabytes. -->
<string name="megabyteShort">MB</string>
<!-- Suffix added to a number to signify size in gigabytes. -->
<string name="gigabyteShort">GB</string>
<!-- Suffix added to a number to signify size in terabytes. -->
<string name="terabyteShort">TB</string>
<!-- Suffix added to a number to signify size in petabytes. -->
<string name="petabyteShort">PB</string>
  1. android.media.ThumbnailUtils類,用來獲取媒體(圖片、視頻)縮略圖
  2. String.format(String format, Object... args)Context.getString(int resId, Object... formatArgs)用于格式化strings.xml中的字符串
  3. TextView.append(CharSequence text)append(CharSequence text, int start, int end)方法,直接在Textview后面追加字符串
  4. DecimalFormat類,用于字串格式化包括指定位數、百分數、科學計數法等
  5. android.util.Pair類,封裝了兩個對象的類(用處自己想吧)
public Pair(F first, S second) {    
       this.first = first;    
       this.second = second;
}
  1. 安卓的單元測試類 ApplicationTestCase<Application>自帶mContext屬性,InstrumentationTestCase可以跳轉到一個Activity進行測試
  2. Android 軟鍵盤蓋住輸入框或者布局的解決辦法
  • 方法一:在你的activity中的oncreate中setContentView之前寫上這個代碼'getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);'
  • 方法二:在項目的AndroidManifest.xml文件中界面對應的<activity>里加入android:windowSoftInputMode="stateVisible|adjustResize",這樣會讓屏幕整體上移。如果加上的是android:windowSoftInputMode="adjustPan"這樣鍵盤就會覆蓋屏幕。
  • TextView的標準字體
style="@style/TextAppearance.AppCompat.Display4"
style="@style/TextAppearance.AppCompat.Display3"
style="@style/TextAppearance.AppCompat.Display2"
style="@style/TextAppearance.AppCompat.Display1"
style="@style/TextAppearance.AppCompat.Headline"
style="@style/TextAppearance.AppCompat.Title"
style="@style/TextAppearance.AppCompat.Subhead"
style="@style/TextAppearance.AppCompat.Body2"
style="@style/TextAppearance.AppCompat.Body1"
style="@style/TextAppearance.AppCompat.Caption"
style="@style/TextAppearance.AppCompat.Button"
樣式效果
  • android:clipToPadding控件的繪制區域是否在padding里面。默認為true。如果你設置了此屬性值為false,可以實現 ListView頂部默認有一個間距,向上滑動后,間距消失。(PS:如果使用margin或padding,都不能實現這個效果。加一個headerView又顯得大材小用,而且過于麻煩。此處的clipToPadding配合paddingTop效果就剛剛好。)

  • 類似上面android:clipChildren的意思:否限制子View在其范圍內。在做動畫或者一些特別的布局是會起到非常神奇的作用

  • Android5.0以后Theme的一些顏色


  • CountDownTimer可以用于倒計時

  • 通過Calendar獲得第幾周的周幾

final int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
final int dayOfWeekInMonth = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
  • 在layout.xml中設置RecyclerView.setLayoutManager()
xml布局文件

to be continue...

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容