2013-12-15 103 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財產。