2012-07-24 109 views
1

我正在開發一款Android 2.2.2應用程序,它將支持多種屏幕尺寸,並且所有屏幕都將是人像。我不會支持風景。相同的佈局在三星Galaxy Tab上看起來不同

我在HTC Desire和Samsung Galaxy Tab 7.7上測試了以下佈局。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/no_conectado" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/labelSelGateName" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:layout_marginTop="80dp" /> 

    <TextView 
     android:id="@+id/labelSelOpened" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" /> 

    <ProgressBar 
     android:id="@+id/indicatorActivityView" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_marginTop="22dp" 
     android:layout_gravity="center_horizontal" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="110dp" 
     android:layout_marginTop="60dp" 
     android:orientation="horizontal" > 

     <ImageButton 
      android:id="@+id/btnMyGates" 
      android:layout_width="50dp" 
      android:layout_height="110dp" 
      android:layout_marginLeft="20dp" 
      android:layout_weight=".33" 
      android:background="@null" 
      android:contentDescription="@string/layout_empty" 
      android:onClick="onGateClick" /> 

     <LinearLayout 
      android:layout_width="90dp" 
      android:layout_height="110dp" 
      android:layout_weight=".33" 
      android:orientation="vertical" > 

      <ImageButton 
       android:id="@+id/btnOpen" 
       android:layout_width="90dp" 
       android:layout_height="55dp" 
       android:layout_gravity="center_horizontal" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:background="@null" 
       android:contentDescription="@string/layout_empty" 
       android:onClick="onOpenDoorClick" /> 

      <ImageButton 
       android:id="@+id/btnClose" 
       android:layout_width="90dp" 
       android:layout_height="50dp" 
       android:layout_gravity="center_horizontal" 
       android:layout_marginTop="10dp" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:background="@null" 
       android:contentDescription="@string/layout_empty" 
       android:onClick="onCloseDoorClick" /> 

     </LinearLayout> 

     <ImageButton 
      android:id="@+id/btnOptions" 
      android:layout_width="50dp" 
      android:layout_height="110dp" 
      android:layout_marginRight="20dp" 
      android:layout_weight=".33" 
      android:background="@null" 
      android:contentDescription="@string/layout_empty" 
      android:onClick="onOptionClick" /> 

    </LinearLayout> 

    <ImageButton 
     android:id="@+id/btnFaqs" 
     android:layout_width="110dp" 
     android:layout_height="60dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="10dp" 
     android:background="@null" 
     android:contentDescription="@string/layout_empty" 
     android:onClick="onFAQClick" /> 

    <ImageButton 
     android:id="@+id/btnInfo" 
     android:layout_width="110dp" 
     android:layout_height="60dp" 
     android:layout_gravity="right" 
     android:layout_marginTop="20dp" 
     android:background="@null" 
     android:contentDescription="@string/layout_empty" 
     android:onClick="onInfoClick" /> 

</LinearLayout> 

我有ldpi,mdpi,hdpi和x-hdpi的圖片。

背景圖像看起來不錯,但是所有小工具(TextView,ProgressBar,ImageButton等)在Samsung Galaxy Tab上測試時都不在正確的位置。

我在Eclipse上使用'Nexus One'作爲模型設計了這個佈局。

Here有人推薦我對每種屏幕尺寸和密度只使用一種佈局,但它不起作用。我使用的是dp單元和fill_parent等,但它在Galaxy Tab上不同。

我需要x-large屏幕尺寸的佈局嗎?

+0

很糟糕的佈局設計。 – 2012-07-24 07:48:31

+0

@PadmaKumar你推薦我什麼? – VansFannel 2012-07-24 07:53:54

+1

使用相對佈局而不使用硬編碼。 – 2012-07-24 08:00:20

回答

6

事實上,你收到的建議是很好的:它可能只有一個佈局文件,但因爲它是在評論已經建議,這不是很好硬編碼尺寸,即使您使用dpdip,特別是當您定位所有可用的屏幕尺寸和密度時。

相反,你應該鏈接到尺寸替換這些值的值

例如,而不是android:layout_marginLeft="10dp",你必須像

android:layout_marginLeft="@dimen/textview_margin_left" 

其中textview_margin_left在dimens.xml定義,其在不同的文件夾不同的值;
大概在文件夾<dimen name="textview_margin_left">10dp</dimen>
文件夾價值觀大<dimen name="textview_margin_left">20dp</dimen>
價值觀XLARGE<dimen name="textview_margin_left">30dp</dimen>

但是,這只是一個例子,你必須測試所有尺寸和分辨率,併爲您的佈局找到最佳值。在EclipseGraphical Layout模式中,只需點擊Nexus One,然後從列表中選擇另一個型號,即可自動更新佈局,您可以輕鬆瞭解佈局在各種設備上的外觀。

此外,您可以移動dimens.xml所有文本大小,這對於大型設備非常有用。

只使用一個RelativeLayout而不是許多分層LinearLayouts也可能是一個好主意,並且使用相對定位來表示對象,而不是某些硬編碼值。

+0

非常感謝您的回答。它幫助我很多! – VansFannel 2012-07-24 14:34:39

+0

很高興爲您服務! – Adinia 2012-07-24 14:48:00

+0

我已經完成了你所說的內容:在'values-small','values-normal','values-large'和'values-xlarge'中使用RelativeLayout和四個'dimens.xml'進行佈局。有了這些文件,Galaxy Nexus,5.4 FWVGA,5.1 WVGA和4.7英寸WXGA都不太好看。我需要更多'dimens.xml'嗎?但是'Nexus One','7in WSVGA Tablet'和'10.1in WXGA Tablet'(我在談論Eclipse中的圖形佈局)看起來不錯。 – VansFannel 2012-07-24 15:26:42

1

問題是你在佈局中使用靜態值(100dp,60dp)。現在問題是在更高的分辨率,這個DP是不一樣的。

這意味着您應該爲x大屏幕創建佈局。我也不會使用這些靜態值。比你的應用程序在許多不同的屏幕尺寸上表現良好!

有一個偉大的日子

野生動物園

+0

你會使用什麼值? – VansFannel 2012-07-24 07:47:33