2013-04-22 133 views
10

创建一个简单的XML设计如下,当我面临着一个奇怪的问题在我的项目之间安卓布局设计:差异2.3.3版本和4+

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="#FF0000" 
    android:layout_marginLeft="30dp" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button" 
     android:layout_gravity="left|center_vertical"/> 

现在看到了差距,这是在4.2.2视图:

2.3.3 version

而这一次在2.3.3

enter image description here

希望如果有人可以帮我一下吧。感谢

+0

出于好奇,你为什么要设置layout_gravity left和center_vertical? – FoamyGuy 2013-04-22 14:14:49

+0

呃...那有什么区别?你想让它“被完整”吗? – Ahmad 2013-04-22 14:14:57

+1

@Ahmad黑吧(大概是边缘)是在相反的两侧 – FoamyGuy 2013-04-22 14:15:29

回答

3

它的工作原理,如果你把它改成这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    android:layout_marginLeft="30dp" 
    android:background="#FF0000" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:text="New Button" /> 

</LinearLayout> 

(我想我知道为什么它的行为就是这样,让我先确认一下,我将添加后面的解释)

+0

嗨艾哈迈德,这只解决了这里提出的问题的真实答案。在我的项目中,我有非常复杂的布局,不能以这种方式改变。等待你的解释。谢谢 – 2013-04-22 14:26:37

+0

@UdiOshi对不起,我的假设是错误的(或者至少我找不到任何证据支持它)。但是,使用任何重力都可以将layout_gravity设置为顶层布局。 – Ahmad 2013-04-22 14:50:40

+0

感谢艾哈迈德,但在我的其他复杂的布局,如果我将设置布局的重力,它会破坏一切:( – 2013-04-22 14:56:47

0

我也面临着同样的问题,在我来说,我有这样的事情

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    style="@style/match" 
    android:background="@color/light" 
    android:orientation="vertical" 
    android:layout_margin="@dimen/lateral_margin" > 

    <ImageView 
     android:id="@+id/full_screen_image" 
     style="@style/match_width" 
     android:scaleType="fitCenter" 
     android:layout_gravity="center" 
     android:layout_weight="2" 
     android:contentDescription="@string/image_content_description"/> 

    <TextView 
     android:id="@+id/barcode_number" 
     style="@style/match_width" 
     android:gravity="center" 
     android:textColor="@color/dark_text" 
     android:textSize="30sp" 
     android:visibility="gone" /> 

</LinearLayout> 

我的问题解决了改变

android:layout_margin="@dimen/lateral_margin" > 

有:

android:padding="@dimen/lateral_margin" > 

看来机器人2.3.3及以下并比Android 4和更大的不同计算保证金。现在他们都以同样的方式行事。

在你的情况,可能会设置黑色布局边距作为填充可以帮助。