2011-10-25 16 views
10

I次のXMLがありますRelativeLayout - CenterInParentとmarginTop

<RelativeLayout android:id="@+id/cover_box" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"> 
    <ImageView android:id="@+id/cover" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
    <ImageView android:id="@+id/download" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:src="@drawable/mark_download" 
     android:layout_centerInParent="true" android:layout_marginTop="90px" /> 
</RelativeLayout> 

をしかし、それはmarginTopは無視されているようなものだと見ています。

答えて

8

親を中心に使用すると、ビューは中央に直接配置されます。マージントップは、オブジェクトがビューの最上部の90ピクセル以内にある場合にのみ発生します。したがって、中央に表示されたビューを押し下げて、その上に少なくとも90ピクセルのスペースを確保します。それは無視されていませんが、それはあなたが持っている必要があると思う効果がありません。

+0

それがそうであった場合は、にマージンを変更値を大きくすると画像の位置が変わります。しかし、私はAndroid 4.2でテストしています.centerInParentが相対レイアウトのマージンを完全に上書きするようです。 – Behnam

24

2番目の画像を画面中央から90dpにするには、画像を下に移動するためにパディングを制御できるFrameLayoutに置き換えることができます。

<FrameLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:paddingTop="90dp"> 
    <ImageView android:id="@+id/download" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/mark_download"/> 
</FrameLayout> 
+1

これは非常に役に立ちました。ありがとうございました。中央にframelayoutとパディングを追加/ –

+0

これは受け入れられた答えでなければなりません –

3

私はandroid:layout_centerInParent="true"下に表示するプログレスバーはそう、私はそれの下で私のプログレスバーを入れたダミーTextViewを追加し、centerInParent .Thenに設定していたいです。今度は、中心からの距離を2つの方法で増やすことができます。最初にTextViewでmarginTopを増やし、TextViewの高さを2番目に増やします。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/widget" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/splash" > 

    <FrameLayout 
     android:id="@+id/splash_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <TextView 
     android:id="@+id/txt1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 

    <com.s3.tdd.interfaces.CircularProgressBar 
     android:id="@+id/circularprogressbar2" 
     style="@style/Widget.ProgressBar.Holo.CircularProgressBar" 
     android:layout_width="110dip" 
     android:layout_height="110dip" 
     android:layout_below="@+id/txt1" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 
0

ダミービューを作成して親の中央に配置することができます。レイアウトを使用してダミービューに相対的なビューを整列します:alignComponentとmarginTopを与えます。コード内で、ビューの幅に合わせて中心に移動します。

1

あなたはImageViewのマージンを残し、そしてのViewGroupのためandroid:centerInParent="true"をやって、他のViewGroup(RelativeLayoutレイアウトののLinearLayout)の内側にあなたのImageViewのを置くことができます。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:centerInParent="true"> 
      <ImageView android:id="@+id/download" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/mark_download" android:layout_marginTop="90px" />  
    </LinearLayout>  
</RelativeLayout> 
関連する問題