1. 程式人生 > >Android AndroidManifest.xml檔案的android:supportsRtl屬性詳解

Android AndroidManifest.xml檔案的android:supportsRtl屬性詳解

Android Studio新建工程的AndroidManifest檔案裡會有一個supportsRtl屬性,並且預設是true,那這個屬性到底有什麼用呢,顧名思義就是“支援RTL”,那RTL又是神馬鬼

帶著這個問題去翻了翻Android官網的文件

這是官網對android:supportsRtl的解釋,英文不太好只能用工具和自己的理解翻譯一下

android:supportsRtl

Declares whether your application is willing to support right-to-left (RTL) layouts.

If set to true and targetSdkVersion is set to 17 or higher, various RTL APIs will be activated and used by the system so your app can display RTL layouts. If set to false or if targetSdkVersion is set to 16 or lower, the RTL APIs will be ignored or will have no effect and your app will behave the same regardless of the layout direction ass ociated to the user's Locale choice (your layouts will always be left-to-right).

The default value of this attribute is false.

This attribute was added in API level 17.

宣告你的application是否願意支援從右到左(原來RTL就是right-to-left 的縮寫...)的佈局。

如果設定為true,targetSdkVersion設定為17或更高,各種RTL的API將被啟用,系統使用您的應用程式可以顯示RTL佈局。如果targetSdkVersion設定為16或更低的設定為false,RTL的API將被忽略或沒有影響您的應用程式將具有相同的行為無論對使用者現場的選擇相關的佈局方向(你的佈局會從左至右)。
此屬性的預設值是false。

此屬性被新增到API 17。


最後一句也說了,這個api是17(也就是Android4.2)以後才有的,並且這個屬性預設是false,17以前的api不支援這個屬性。

那這個支援從右到左的佈局到底是個什麼鬼呢。

經常玩機的朋友應該有發現,在設定-開發人員選項,有一個強制使用從右到左的佈局方向,如圖


既然有這麼個東西,那就開啟看看


開啟之後變成這樣了,原先在左邊的文字被放到右邊去了,在右邊的開關,也被放到左邊了,看到這個就明白了這個屬性的意思了吧

為了證明這個屬性,再弄個demo試試

當android:supportsRtl為false的時候,即使手機開啟了強制從右到左,app的佈局也不會改變,如圖


當android:supportsRtl為true的時候,並且手機也開啟了強制從右到左的開關,那麼佈局就會從右到左來排列了,如圖