2011-10-17 42 views
1

我創建了一個預置(480x800)密度的佈局。自從我開始學習android(9個月)以來,我一直在使用它,現在是時候在其他手機上測試我的應用了。我用三個480x800分辨率的手機測試了它,並且沒有問題,直到我用320x480和240x320的一個測試它。我使用px-s作爲寬度和高度,paddingTop等等。 我在模擬器中檢查了應用程序(爲不同的分辨率創建了不同的avd-s),我看不到整個佈局,因爲它比屏幕更大(僅在eclipse中測試它)。它有4個帶有「wrap_content」寬度和高度設置的圖像。 所以我檢查了android文檔。我沒有創建其他佈局或其他任何東西,但用dp-s替換了px-s。這是相同的。 我創建了190x60px分辨率的較小按鈕(見下文),並將它們放到ldpi文件夾中,但沒有大的進展。也許是因爲文本瀏覽的文本大小是相同的,並且在240x320分辨率的情況下,2個文本瀏覽佔據了顯示位置的1/3(而在480x800的情況下只有1/6)。 (因此,與較大分辨率相比,小分辨率下的文字看起來很大) 請告訴我該如何使320x480分辨率的佈局看起來像480x800一樣。android密度獨立

gradientbg的大小:這是一個形狀的.xml文件,所以沒有物理大小。 按鈕的大小(圖像):380x150px HDPI(或190x60px在LDPI文件夾)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/gradientbg" 
    > 
    <TextView 
     android:id="@+id/TextView00" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#E30000" 
     android:textColor="#FFFFFF" 
     android:gravity="center_horizontal|center_vertical" 
     android:textSize="26dp" 
     android:height="40dp" 
     android:textStyle="bold" 
     android:text="Main menu" 
    /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="App" 
     android:textSize="30dp" 
     android:textStyle="bold" 
     android:paddingTop="20dp" 
     android:layout_gravity="center_horizontal" 
     android:textColor="#FFB300" 
    /> 
    <Button android:id="@+id/btn1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
      android:background="@drawable/mainbutton_1" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="20dip" 
    /> 
    <Button android:id="@+id/btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/mainbutton_2" 
     android:layout_gravity="center_horizontal" 
     android:paddingTop="5dp" 
    /> 
    <Button android:id="@+id/btn3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
       android:background="@drawable/mainbutton_3" 
     android:layout_gravity="center_horizontal" 
     android:paddingTop="5dp" 
    /> 
    <Button android:id="@+id/btn4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
       android:background="@drawable/mainbutton_4" 
     android:layout_gravity="center_horizontal" 
     android:paddingTop="5dp" 
    /> 

</LinearLayout> 
+0

爲不同的尺寸創建不同的佈局。你可以看看這個問題:http://stackoverflow.com/questions/5726764/android-layouts-for-multiple-screen-sizes –

+1

像素密度和屏幕尺寸是完全不同的東西。如果你把資源放在ldpi文件夾中,它們不一定會在小屏幕上使用。爲此,您需要將它們放在小文件夾中。有關更多信息,請參閱主題[支持多個屏幕](http://developer.android.com/guide/practices/screens_support.html)。 –

+0

好的。哪一個更好?要創建不同大小的按鈕(圖像),或足夠(右)將寬度和高度設置爲比例(例如,hdpi爲380dp,ldpi爲190dp)大小。最後一個更容易和有效,但我很好奇。 – erdomester

回答

0

當您通常定義在dp單位的佈局,可以確保佈局保持不變在相同密度剷鬥裝置。但是,當您在平板電腦(xlarge)或小屏幕上嘗試它時,它不會正確縮放。此工具可讓您的應用程序適用於所有設備(小型/普通/大型/大型)。它根據您最初設計的基準密度來擴展您的佈局xml文件。

http://onemanmobile.blogspot.com/2012/04/how-to-scale-your-android-layouts-to.html