2012-07-24 86 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

相关问题