更新於2014-04-09 html
35. 若是ViewPager顯示不出來看是否是沒有設置高度,由於對於ViewPager來講,設置成wrap_content是不行的.要麼是match_parent要麼呢,指定具體的高度. java
更新於2013-08-27 python
34. include進來的佈局layout_margin不起做用。 linux
解決方法須要同時設置layout_width和layout_height android
see:http://stackoverflow.com/questions/11947987/margin-does-not-impact-in-include ios
最後更新:2012-08-15 web
33.設置EditText的inputType爲優先使用數字鍵。 正則表達式
好比密碼框,有些場景是使用純數字的比較多,可是密碼仍是可使用其它符號的。因此限定inputType爲number的話確定不行。爲text就跟沒有設置同樣。因此。可使用以下設置來解決這個問題: shell
passwordView.setRawInputType(Configuration.KEYBOARD_QWERTY);
更新:2012-06-06: api
32.在EditText中將光標放在文本後面。參考
EditText et = (EditText)findViewById(R.id.inbox); et.setSelection(et.getText().length());
http://stackoverflow.com/questions/6217378/place-cursor-at-the-end-of-text-in-edittext
最後更新:2012-05-19:
31.使用android和浮點工具類而不是java.lang.Math的
Use android.util.FloatMath#ceil() instead of java.lang.Math#ceil to avoid argument float to double conversion Issue: Suggests replacing java.lang.Math calls with android.util.FloatMath to avoid conversions Id: FloatMath On modern hardware, "double" is just as fast as "float" though of course it takes more memory. However, if you are using floats and you need to compute the sine, cosine or square root, then it is better to use the android.util.FloatMath class instead of java.lang.Math since you can call methods written to operate on floats, so you avoid conversions back and forth to double. http://developer.android.com/guide/practices/design/performance.html#avoidfloat
30.android開發工具竟然還能檢測,你一樣資源的重要啊,哈哈.
The following unrelated icon files have identical contents: btn_default_focused_holo_dark.9.png, btn_default_focused_holo_light.9.png Issue: Finds duplicated icons under different names Id: IconDuplicates If an icon is repeated under different names, you can consolidate and just use one of the icons and delete the others to make your application smaller. However, duplicated icons usually are not intentional and can sometimes point to icons that were accidentally overwritten or accidentally not updated.
29.檢查無用的資源:
The resource R.drawable.add_fastlink_bg appears to be unused Issue: Looks for unused resources Id: UnusedResources Unused resources make applications larger and slow down builds.
28.使用新的prograud配置方案.
Local ProGuard configuration contains general Android configuration: Inherit these settings instead? Modify project.properties to define proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard.cfg and then keep only project-specific configuration here Issue: Checks for old proguard.cfg files that contain generic Android rules Id: ProguardSplit Earlier versions of the Android tools bundled a single "proguard.cfg" file containing a ProGuard configuration file suitable for Android shrinking and obfuscation. However, that version was copied into new projects, which means that it does not continue to get updated as we improve the default ProGuard rules for Android. In the new version of the tools, we have split the ProGuard configuration into two halves: * A simple configuration file containing only project-specific flags, in your project * A generic configuration file containing the recommended set of ProGuard options for Android projects. This generic file lives in the SDK install directory which means that it gets updated along with the tools. In order for this to work, the proguard.config property in the project.properties file now refers to a path, so you can reference both the generic file as well as your own (and any additional files too). To migrate your project to the new setup, create a new proguard-project.txt file in your project containing any project specific ProGuard flags as well as any customizations you have made, then update your project.properties file to contain: proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
27.關於使用設備獨立的圖片:
The image table_getcheck.png varies significantly in its density-independent (dip) size across the various density versions: drawable-hdpi\table_getcheck.png: 65x60 dp (97x90 px), drawable-mdpi\table_getcheck.png: 48x45 dp (48x45 px) Issue: Ensures that icons across densities provide roughly the same density-independent size Id: IconDipSize Checks the all icons which are provided in multiple densities, all compute to roughly the same density-independent pixel (dip) size. This catches errors where images are either placed in the wrong folder, or icons are changed to new sizes but some folders are forgotten.
26.使用SparseIntArray來提高性能,而不是使用HashMap<Integer,Integer>
Use new SparseIntArray(...) instead for better performance Issue: Looks for opportunities to replace HashMaps with the more efficient SparseArray Id: UseSparseArrays For maps where the keys are of type integer, it's typically more efficient to use the Android SparseArray API. This check identifies scenarios where you might want to consider using SparseArray instead of HashMap for better performance. This is *particularly* useful when the value types are primitives like ints, where you can use SparseIntArray and avoid auto-boxing the values from int to Integer. If you need to construct a HashMap because you need to call an API outside of your control which requires a Map, you can suppress this warning using for example the @SuppressLint annotation.
25.使用靜態工廠方法而不是new.
Use Integer.valueOf(temp & 0xff) instead Issue: Looks for usages of "new" for wrapper classes which should use "valueOf" instead Id: UseValueOf You should not call the constructor for wrapper classes directly, such as"new Integer(42)". Instead, call the "valueOf" factory method, such as Integer.valueOf(42). This will typically use less memory because common integers such as 0 and 1 will share a single instance.
24.用一基類的Activity的子類:
The <activity> cn.ditouch.client.activity.BaseFragmentActivity is not registered in the manifest Issue: Ensures that Activities, Services and Content Providers are registered in the manifest Id: Registered Activities, services and content providers should be registered in the AndroidManifext.xml file using <activity>, <service> and <provider> tags. If your activity is simply a parent class intended to be subclassed by other "real" activities, make it an abstract class. http://developer.android.com/guide/topics/manifest/manifest-intro.html
23.android肯定及取消按鈕順序
看下運行android lint以後的一個提示吧.:
Layout uses the wrong button order for API >= 14: Create a layout-v14/connect_server.xml file with opposite order: OK button should be on the right (was "OK | Cancel", should be "Cancel | OK")
最後更新 :2012-04-28
22.在android中使用聲音效果
Thanks to:http://stackoverflow.com/questions/5236709/sound-effects-on-button-click
最佳答案:
You can use a SoundPool, look here: http://developer.android.com/reference/android/media/SoundPool.html Load the sounds you need into your SoundPool and then use the play() method.
http://www.droidnova.com/creating-sound-effects-in-android-part-2,695.html
寫得比較詳細.比較好請參考 .
2012-04-22
21.取得當前系統的配置信息,及應用使用了
Configuration cfg = getApplication().getResources().getConfiguration(); int sz = cfg.screenLayout&cfg.SCREENLAYOUT_SIZE_MASK; System.out.println("size: "+sz);
20,用於ipv4的正則表達式:
^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[09][0-9]|[1-9][0-9]|[0-9])$
Thanks to:http://demon.tw/programming/regex-ipv4.html
19.在ListView項中添加的Button等控件以後要注意的地方:
(1)控件的onClickListener()必要在getView()方法中註冊.
(2)要想 ListView中的item還可使用onItemClickListener()和onItemLongClickListner()的話,須要將
listView中的item佈局中的Button等控件設置爲focusable="false"
Thanks to: (1)http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html
(2) http://txlong-onz.iteye.com/blog/907186
18. 如何檢索android設置的惟一ID
Thanks to:http://articles.csdn.net/badasanxingzhuanqu/jishuwenzhang_Android/2011/0919/304625.html
對於2.2及之後的版本,使用下面的方法簡單直接:
ANDROID_ID
說明
更具體地說,Settings.Secure.ANDROID_ID 是一串64位的編碼(十六進制的字符串),是隨機生成的設備的第一個引導,其記錄着一個固定值,經過它能夠知道設備的壽命(在設備恢復出廠設置後,該值可能會改變)。ANDROID_ID也可視爲做爲惟一設備標識號的一個好選擇。如要檢索用於設備ID 的ANDROID_ID,請參閱下面的示例代碼
String androidId = Settings.Secure.getString(getContentResolver(),Settings.Secure.ANDROID_ID);
缺點
• 對於Android 2.2(「Froyo」)以前的設備不是100%的可靠
• 此外,在主流製造商的暢銷手機中至少存在一個衆所周知的錯誤,每個實例都具備相同的ANDROID_ID。
17.高效的適配器 Efficient Adapter
Thanks to :android docs:docs/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
使用ViewHolder 模式
具體使用參見上面參考文檔.
16.接管BACK按鈕事件
Thanks to :http://stackoverflow.com/questions/2000102/android-override-back-button-to-act-like-home-button
@Override public void onBackPressed(){ // do something check // you can also do what onBackPressed do just call super.onBackPressed(); }
15. 在View中攜帶數據的方式:
使用 View.setTag(Object)
14.檢測網絡鏈接狀態.
(1 ) 首先確保配置了相應權限.使用以下權限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />(2)檢測的網絡鏈接狀態的示例代碼
private boolean checkNetwork() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo == null || !networkInfo.isAvailable()) { // showDialog return false; } else if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI) { Toast.makeText(this, "當前網絡鏈接使用手機流量!,建議使用Wifi!", Toast.LENGTH_LONG); } return true; }
13.設置對話框風格的Acitivity.
只要在acititty屬性設置中設置使用對話框主題就能夠了.以下:
android:theme="@android:style/Theme.Dialog"
12. 使用include,你能夠在書上或者其它地方看到使用include的一個好處就是便於重用.
可是我發現了一個問題,就是當你的界面佈局很複雜的時候,佈局xml都在一個文件裏,使用eclipse的圖形佈局來預覽的時候,根本看來出效果來.得真機才行.可是當佈局簡單的時候,就能夠了.
有一次我是把一個佈局的部分copy出來,調試好了以後再放進去,可是你知道嗎?這個也很麻煩的.copy來copy去.
解決辦法 就是include了.而後就不用copy來copy去了.
個人複雜佈局就是由於使用了SlidingDrawer哈哈,下面是引用 handler
<include layout="@layout/order_list_drawer_handler"/>
11. 與TextView有關的佈局的優化
在我使用一個佈局的時候,eclipse提示了一個警告,我看到以下說明:
This tag and its children can be replaced by one <TextView/> and a compound drawable
而後我搜索了這句話:
在stackoverflow中也有人提到了這個問題:上面說明的很清楚了:
由於TextView能夠設置相似android:drawableLeft,android:drawableRight等等屬性.
(我暈一下,個人eclipse竟然沒有提示上面的這些屬性.)
其實,主要一點就是,不要小看了TextView.
從代碼行解釋這個問題能夠參見:
TextView碉堡了!android源代碼的一些統計信息!
10.設置自定義的標題欄,
使用以下代碼:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);R.layout.titlebar以下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:background="@drawable/title_bar_bg"> <TextView android:id="@+id/title_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="26sp" android:gravity="center" /> </LinearLayout>有一個問題,我沒法使用TextView titleBar = (TextView)findViewById(R.id.titlebar)來得到這個titleBar
可是我使用setTitle仍是會生效的.(若是我這個layout裏面沒有TextView會報錯嗎?)
9.關於佈局與焦點:
我發現android處理Layout中的View樹的焦點是按照他在Layout中佈局順序來處理的.
例如,在一個相對佈局的中個人一個SlidingDrawer想放在最上面使用alignParentTop,下面是一個GridView.
在運行的結果中,GridView會首先獲得焦點.並且看起來就會將SlidingDrawer覆蓋掉.這個SlidingDrawer也就沒法下拉.
可是以下設置GridView在SlidingDrawer的下面的話,那麼GridView將顯示不出來.暫時不清楚問題本質緣由.
哈哈,剛開始我是在GridView上面設置一個比較大的marginTop可是這個還不能很好的解決問題,
爲此我花了N多的時間來組合不一樣的佈局,想到過用FrameLayout,FrameLayout確實可使用.
可是,後來嘗試到使用Relative也是能夠的.可是我將SlidingDrawer放到GridView的下面.因而SlidingDrawer將覆蓋
在GridView的頂部,可是這個沒有關係.哈哈.
8. 一個androidView類未公開的能夠用於調試View的API。能夠打印出View類比較詳細有針對性的消息。
/** * Prints information about this view in the log output, with the tag * {@link #VIEW_LOG_TAG}. * * @hide */ public void debug() { debug(0); }號外,我發現android2.3.2中View類的代碼爲9800多行。而在android4.0.3中代碼有15000行。
這個真的傷不起啊。
事實上通過統計排名前十的類文件以下:
banxi1988@banxi:~/android/tmp/android_api_15/android$ find . -type f -name '*.class' -exec du -h {} \;|sort -nr|head 56K ./android/view/View.class 48K ./android/R$attr.class 44K ./android/content/Intent.class 36K ./android/widget/TextView.class 36K ./android/R$style.class 32K ./android/app/Activity.class 28K ./android/widget/AbsListView.class 28K ./android/webkit/WebView.class 28K ./android/view/ViewGroup.class 28K ./android/view/KeyEvent.class
1.讓界面默認使用橫屏(配置android:screenOrientaion),而且不隨自動改變(指定哪些配置變化程序本身處理android:configChanges),以下:
<activity android:name=".OrderClientActivity" android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation" />2.ViewFlipper顯示指定的View(使用setDisplayedChild())
mFlipper.setDisplayedChild(position);3.配置某一個Activity窗口特性如無標題欄,全屏:
參考來自:http://daixu-yang.iteye.com/blog/1096803
(1)能夠經過設置活動的主題android:theme爲android內部的某些具備無標題欄的特性的主題:以下:
android:theme="@android:style/Theme.Light.NoTitleBar"關鍵是有NoTitleBar這個的就能夠了。
其它的屬性如全屏也是這樣設置。
(2)經過代碼:
如設置無標題欄,要在setConentView()以前調用:
requestWindowFeature(Window.FEATURE_NO_TITLE);設置全屏:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
4. 啊手賤,我人R文件不見了:
事出原由:是由於我以爲原來項目的包名起得不對因而修改了,沒有報錯,可是運行的時候報錯了,提示找不到類啊。
而後我就仔細看了下,發現他找的仍是原來包名的類,因而我仔細看了下,R類文件 所在包的名字就是仍是原來的包名。
因而修改之,可是仍是不能運行。而後悲劇發生了我發現R類不見了。而後我見Manfifest文件中就是有定義包名,因而 手動改之,而後R文件真的不見了,悲劇啊,因而我不斷的clear,重啓。最後。
我仍是將清單文件中的package改回原來的名字,哈哈,這個出來了,因而我將項目類文件一個一個引用 錯誤。
(由於不知道怎麼的,類中R引用 變成了android.R了。)
而後在項目中使用right click->android tool-> Rename Application Package.
哈哈,這樣就能夠修改包了。應該要這樣 作啊。
結果 手賤啊。改了以後 。
5. 得到LayoutInflater和兩種方式:
(1)LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
(2) LayoutInflater mInflater = mInflater = LayoutInflater.from(context);
7. 在我前面的文章:
http://my.oschina.net/banxi/blog/49210
中寫到了在查看源代碼的時候發現了android有使用@hide的規則。
因而我grep了下源代碼看下有還有哪些沒有公開的能夠公用的API。
banxi1988@banxi:~/android/android-sdk-linux_x86/sources/android-15/android$ grep -l -r @hide . > hide_api.txt這樣我就能夠看到有多少沒有公開的API了。顯然我上面是grep了android-15的源代碼。