Andriod 学习笔记 layout布局
一:不同的layout
Android手機屏幕大小不一,有480x320, 640x360,
800x480.怎樣才能讓App自動適應不同的屏幕呢?
??
其實很簡單,只需要在res目錄下創建不同的layout文件夾,比如layout-640x360,layout-800x480,所有的layout文件在編譯之后都會寫入R.java里,而系統會根據屏幕的大小自己選擇合適的layout進行使用。
二:hdpi、mdpi、ldpi
在之前的版本中,只有一個drawable,而2.1版本中有drawable-mdpi、drawable-ldpi、drawable-hdpi三個,這三個主要是為了支持多分辨率。
drawable- hdpi、drawable- mdpi、drawable-ldpi的區別:
(1)drawable-hdpi里面存放高分辨率的圖片,如WVGA (480x800),FWVGA (480x854)
(2)drawable-mdpi里面存放中等分辨率的圖片,如HVGA (320x480)
(3)drawable-ldpi里面存放低分辨率的圖片,如QVGA (240x320)
系統會根據機器的分辨率來分別到這幾個文件夾里面去找對應的圖片。
更正:應該是對應不同density 的圖片 ?
?
在開發程序時為了兼容不同平臺不同屏幕,建議各自文件夾根據需求均存放不同版本圖片。
[i]備注:三者的解析度不一樣,就像你把電腦的分辨率調低,圖片會變大一樣,反之分辨率高,圖片縮小。[/i]
屏幕方向:
橫屏豎屏自動切換:?
可以在res目錄下建立layout-port-800x600和layout-land兩個目錄,里面分別放置豎屏和橫屏兩種布局文件,這樣在手機屏幕方向變化的時候系統會自動調用相應的布局文件,避免一種布局文件無法滿足兩種屏幕顯示的問題。
不同分辨率橫屏豎屏自動切換:
以800x600為例
可以在res目錄下建立layout-port-800x600和layout-land-800x600兩個目錄
不切換:
以下步驟是網上流傳的,不過我自己之前是通過圖形化界面實現這個配置,算是殊途同歸,有空我會把圖片貼上來。
還要說明一點:每個activity都有這個屬性screenOrientation,每個activity都需要設置,可以設置為豎屏(portrait),也可以設置為無重力感應(nosensor)。
要讓程序界面保持一個方向,不隨手機方向轉動而變化的處理辦法:?
在AndroidManifest.xml里面配置一下就可以了。加入這一行android:screenOrientation="landscape"。
例如(landscape是橫向,portrait是縱向):
Java代碼:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android package="com.ray.linkit"?android:versionCode="1"?android:versionName="1.0">?
?? <application android:icon="@drawable/icon" android:label="@string/app_name">
?? <activity android:name=".Main"?android:label="@string/app_name"?android:screenOrientation="portrait">
??????????? <intent-filter>?
??????????????? <action android:name="android.intent.action.MAIN" />?
??????????????? <category android:name="android.intent.category.LAUNCHER" />
??????????? </intent-filter>?
?? </activity>?
???<activity android:name=".GamePlay"??android:screenOrientation="portrait"></activity>?
<activity android:name=".OptionView"??android:screenOrientation="portrait"></activity>
</application>
??? <uses-sdk android:minSdkVersion="3" />
</manifest>
另外,android中每次屏幕的切換動會重啟Activity,所以應該在Activity銷毀前保存當前活動的狀態,在Activity再次Create的時候載入配置,那樣,進行中的游戲就不會自動重啟了!
有的程序適合從豎屏切換到橫屏,或者反過來,這個時候怎么辦呢?可以在配置Activity的地方進行如下的配置android:screenOrientation="portrait"。這樣就可以保證是豎屏總是豎屏了,或者landscape橫向。
而有的程序是適合橫豎屏切換的。如何處理呢?首先要在配置Activity的時候進行如下的配
置:android:configChanges="keyboardHidden|orientation",另外需要重寫Activity的
onConfigurationChanged方法。實現方式如下,不需要做太多的內容:
@Override?
public void onConfigurationChanged(Configuration newConfig) {???????
super.onConfigurationChanged(newConfig);?
????? if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {?
??????????? // land do nothing is ok?
?????? } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {?
// port do nothing is ok?
}
}
寫一個支持多分辨的程序,基于1.6開發的,建立了三個資源文件夾drawable-hdpi drawable-mdpi
drawable-ldpi,里面分別存放72*72 48*48
36*36的icon圖標文件。當我在G1(1.5的系統)上測試時,圖標應該自適應為48*48才對啊,但實際顯示的是36*36。怎么才能讓其自適應
48*48的icon圖標呢
解決辦法 drawable-hdpi drawable-mdpi
drawable-ldpi改成drawable-480X320
drawable-800X480的多分辨支持的文件夾
實現的方法:
layout_weight屬性的使用,
合適的背景,一般是要足夠大
合理的布局
在AndroidManifest.xml
設置
??? <uses-sdk android:minSdkVersion="7“
系統的計算方式:
File DisplayHardware.cpp
void DisplayHardware::init(uint32_t dpy){
….
??? /* Read density from build-specific ro.sf.lcd_density property
???? * except if it is overridden by qemu.sf.lcd_density.
???? */
??? if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
??????????? LOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
??????????? strcpy(property, "160");???????
}
??? } else {
??????? /* for the emulator case, reset the dpi values too */
??????? mDpiX = mDpiY = atoi(property);????
????}
??? mDensity = atoi(property) * (1.0f/160.0f);
WindowManagerService.java
boolean computeNewConfigurationLocked(Configuration config) {
……
??????? if (mScreenLayout == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
??????????? // Note we only do this once because at this point we don't
??????????? // expect the screen to change in this way at runtime, and want
??????????? // to avoid all of this computation for every config change.
??????????? int longSize = dw;
??????????? int shortSize = dh;
??????????? if (longSize < shortSize) {
??????????????? int tmp = longSize;
??????????????? longSize = shortSize;
? shortSize = tmp;
??????????? }
??????????? longSize = (int)(longSize/dm.density);
??????????? shortSize = (int)(shortSize/dm.density);
??????????? // These semi-magic numbers define our compatibility modes for
??????????? // applications with different screens.? Don't change unless you
??????????? // make sure to test lots and lots of apps!
??????????? if (longSize < 470)?{
? ??????????? // This is shorter than an HVGA normal density screen (which
??????????????? // is 480 pixels on its long side).
??????????????? mScreenLayout = Configuration.SCREENLAYOUT_SIZE_SMALL? | Configuration.SCREENLAYOUT_LONG_NO;
??????????? } else {
??????????????? // Is this a large screen?
??????????????? if (longSize > 640 && shortSize >= 480) {
// VGA or larger screens at medium density are the point
? ??????????????????? // at which we consider it to be a large screen.
???????????????????? mScreenLayout = Configuration.SCREENLAYOUT_SIZE_LARGE;
??????????????? } else {
??????????????????? mScreenLayout = Configuration.SCREENLAYOUT_SIZE_NORMAL;
??????????????????? // If this screen is wider than normal HVGA, or taller
// than FWVGA, then for old apps we want to run in size
??????????????????? // compatibility mode.
??????????????????? if (shortSize > 321 || longSize > 570)
{
??????????????????????? mScreenLayout |= Configuration.SCREENLAYOUT_COMPAT_NEEDED;???????????????????
?? }
??????????????? }
??????????????? // Is this a long screen?
??????????????? if (((longSize*3)/5) >= (shortSize-1)) {
??????????????????? // Anything wider than WVGA (5:3) is considering to be long.
??????????????????? mScreenLayout |= Configuration.SCREENLAYOUT_LONG_YES;
??????????????? } else {
??????????????????? mScreenLayout |= Configuration.SCREENLAYOUT_LONG_NO;
??????????????? }}
??????? }
??????? config.screenLayout = mScreenLayout;
??????? config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
??????? config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
???????
? mPolicy.adjustConfigurationLw(config);
??????? return true;
??? }
??? 轉載自:http://hi.baidu.com/sdfiyon/item/9221282b9d72defb51fd8733
轉載于:https://www.cnblogs.com/loongsoft/archive/2013/01/22/2872351.html
總結
以上是生活随笔為你收集整理的Andriod 学习笔记 layout布局的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nginx+memcached+tomc
- 下一篇: Mvvm模式学习