2016-04-08 24 views
1

我创建了一个非常简单的应用程序,并将它下载到15个不同的设备中。对于他们中的一些布局没有调整大小的问题,出现这样的:Android:为每个设备自动调整大小

Layout that should be in all layouts

但是,当我在山城银河新Plus和在平板电脑爱可视101铜安装应用程序的项目布局都不在我想要的位置。在平板电脑均达到背景不同颜色的看法,并在银河新加是这样的: Layout in the Galaxy Neo Plus

要自动调整大小,在活动XML,我用thse代码:

android:layout_marginTop="125dp" 
android:layout_marginBottom="15dp" 
android:layout_marginRight="20dp" 
android:layout_marginLeft="20dp" 

到设置按钮从上边距/底/左/右

android:textSize="30dp" 

设置按钮的尺寸文本

布局代码我s此:

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/bg" 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Al meglio di:" 
    android:textSize="22dp" 
    android:id="@+id/almegliodi" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="220dp" 
    android:layout_centerHorizontal="true" 
    android:typeface="serif" 
    android:textStyle="bold|italic" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text=" UNO " 
    android:textStyle="bold" 
    android:textSize="35dp" 
    android:textColor="@android:color/white" 
    android:id="@+id/uno" 
    android:background="@android:drawable/dialog_holo_dark_frame" 
    android:onClick="uno" 
    android:layout_below="@+id/almegliodi" 
    android:layout_marginTop="30dp" 
    android:layout_centerHorizontal="true"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text=" TRE " 
    android:textStyle="bold" 
    android:textSize="35dp" 
    android:textColor="@android:color/white" 
    android:id="@+id/tre" 
    android:background="@android:drawable/dialog_holo_dark_frame" 
    android:onClick="tre" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@+id/uno" 
    android:layout_marginTop="30dp"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text=" CINQUE " 
    android:textStyle="bold" 
    android:textSize="35dp" 
    android:textColor="@android:color/white" 
    android:id="@+id/cinque" 
    android:background="@android:drawable/dialog_holo_dark_frame" 
    android:onClick="cinque" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@+id/tre" 
    android:layout_marginTop="30dp"/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="ESCI" 
    android:textStyle="bold" 
    android:textSize="30dp" 
    android:id="@+id/esci" 
    android:background="@android:color/transparent" 
    android:layout_marginBottom="7dp" 
    android:layout_marginLeft="30dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentBottom="true" 
    android:onClick="esci" 
    /> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="INDIETRO" 
    android:textStyle="bold" 
    android:textSize="30dp" 
    android:textColor="@android:color/white" 
    android:id="@+id/indietro" 
    android:background="@android:color/transparent" 
    android:onClick="indietro" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" 
    android:layout_alignTop="@+id/esci" 
    android:layout_marginRight="30dp" 
    android:layout_marginBottom="7dp" 
    /> 
</RelativeLayout 

我搜索的谷歌,但我发现,使用“DP”是自动调整保证金和文本中的所有设备的最佳途径。

我的错误是什么?我需要做什么来解决兼容性问题?

谢谢

+0

请添加您的布局。一般而言,您应该使用带重量的线性布局来修复该问题 – rekire

+0

可能地,您没有使用正确的密度桶。平板电脑的不同于手机。 –

+0

@rekire我添加了我的布局,但只有通过链接,因为我没有足够的声誉,直接发布图像 – 24terminator96

回答

0

下面是一个例子你的布局可能看起来怎么样:

Example

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#005" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="150dp" 
     android:layout_gravity="center_horizontal" 
     android:src="@android:drawable/btn_star_big_on"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_margin="24dp" 
     android:layout_weight="1" 
     android:background="#cfff" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button"/> 

     <android.support.v4.widget.Space 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button"/> 
    </LinearLayout> 
</LinearLayout> 

您要为每个按钮,使您的应用程序翻译应该引起使用@string/的,但基本的布局应该给你正确的想法如何使一个正确缩放的布局。

基本上我使用一些线性布局,只是说如何安排视图(水平或垂直)。然后我告诉包含三个主要按钮的视图,其高度应为零(仅用于性能),布局权重为1的最重要部分(android:layout_weight="1")告诉系统将开放空间拆分为所有视图一个重量属性。所以这个会尽可能地填满你的显示器。其他视图仍然会按照我的要求呈现wrap_content

+0

为什么你在第三个布局中使用android.support.v4.widget.Space来加权?第二,你是否把重量放在布局的一般礼仪上? sace小部件的用途是什么? – 24terminator96

+0

我忘了提及,这基本上是一个不会显示的占位符,但它需要有空间让您在右侧和左侧有一个视图。这是获得这一结果的最快途径。 – rekire

+0

,如果我想从第二个布局和imageview增加边距?我可以使用marginTop?或者线性布局还有其他功能? – 24terminator96