2013-12-15 104 views
0

我想设置我的RelativeLayout在屏幕中心的位置。 RelativeLayout位于ScrollView内部,使用以下代码。问题在于,在我的模拟器(Android 4.2)中效果很好,但如果我在2.3版本上执行它,则RelativeLayout会在屏幕左上角运行。我无法弄清楚为什么会发生这种情况。滚动视图中的中心RelativeLayout

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/background" > 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_marginTop="75dp" 
    android:layout_marginLeft="50dp"> 
    <EditText 
     android:id="@+id/when" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:layout_marginBottom="20dip" 
     android:inputType="textMultiLine" 
     android:scrollHorizontally="false" 
     android:minLines="1" 
     android:maxLines="3" 
     android:imeOptions="actionDone|flagNoEnterAction" /> 
    <EditText 
     android:id="@+id/memory" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dip" 
     android:layout_marginTop="5dip" 
     android:layout_below="@id/when" 
     android:inputType="textMultiLine" 
     android:scrollHorizontally="false" 
     android:minLines="1" 
     android:imeOptions="actionDone|flagNoEnterAction" 
     android:hint="@string/add_memory_hint" 
     android:background="#6656B3D4" /> 
    <Button 
     android:id="@+id/addMemory" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/memory" 
     android:layout_marginTop="20dp" 
     android:padding="13dp"/> 
    <Button 
     android:id="@+id/neverMind" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/addMemory" 
     android:layout_marginTop="20dp" 
     android:padding="13dp"/> 


</RelativeLayout> 

</ScrollView> 

我真的很感谢一些见解。谢谢。

+0

请张贴完整的XML文件...滚动型开始标记在上述失踪 – Prem

回答

1

在您的相对布局中,请尝试添加下面的标记。它应该工作。

android:layout_gravity="center" 

尝试向相对布局和滚动视图添加背景颜色,并查看更改如何影响。仅用于调试目的。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/darker_gray" > 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_marginTop="75dp" 
    android:layout_marginLeft="50dp" 

    android:background="@android:color/black"> 

我试着设备符合OS 4.4.2

0

这样说吧

scrollview 
    linearLayout (gravity: center) 
     YOUR relativelayour 
     /YOUR relativelayour 
    /linearLayout 
/scrollview 

虽然我不明白为什么你想的东西,是一个滚动视图中的中心

0

可能最简单的是取消了margin从由于RelativeLayoutScrollView都没有graviy,AFAIK,因此您的RelativeLayoutLinearLayout中包含android:gravity="center"。这样

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/background" > 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center"> 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <EditText 
     android:id="@+id/when" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:layout_marginBottom="20dip" 
     android:inputType="textMultiLine" 
     android:scrollHorizontally="false" 
     android:minLines="1" 
     android:maxLines="3" 
     android:imeOptions="actionDone|flagNoEnterAction" /> 
    <EditText 
     android:id="@+id/memory" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dip" 
     android:layout_marginTop="5dip" 
     android:layout_below="@id/when" 
     android:inputType="textMultiLine" 
     android:scrollHorizontally="false" 
     android:minLines="1" 
     android:imeOptions="actionDone|flagNoEnterAction" 
     android:hint="@string/add_memory_hint" 
     android:background="#6656B3D4" /> 
    <Button 
     android:id="@+id/addMemory" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/memory" 
     android:layout_marginTop="20dp" 
     android:padding="13dp"/> 
    <Button 
     android:id="@+id/neverMind" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/addMemory" 
     android:layout_marginTop="20dp" 
     android:padding="13dp"/> 


</RelativeLayout> 
</LinearLayout> 
</ScrollView> 

东西我也删除orientationRelativeLayout因为这ViewGroup不具有orientation财产。