Android RelativeLayout-CenterInParent和marginTop

Android RelativeLayout-CenterInParent和marginTop,android,android-layout,android-view,Android,Android Layout,Android View,我有以下XML: <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

我有以下XML:

<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被忽略了。

在父视图中使用“中心”时,视图直接放在中心。只有在对象位于视图顶部90像素范围内时,边距顶部才会起作用。因此,将居中视图向下推,使其顶部保持至少90像素的空间。因此,它不会被忽略,但不会产生您认为它应该具有的效果。

如果您希望第二幅图像位于屏幕中心下方90 dp,可以将其替换为框架布局,您可以控制填充以向下移动图像

<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>

我希望progressbar显示在android:layout\u centerInParent=“true”下,因此我添加了一个虚拟的
TextView
并将其设置为centerInParent。然后我将progressbar放在它下面。现在,您可以通过两种方式增加它与中心的距离。首先增加TextView中的marginTop,然后增加TextView高度

<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>


您可以创建一个虚拟视图,并将其置于父视图的中心。现在使用layout:alignComponent将视图相对于虚拟视图对齐,并给出marginTop。现在,在代码中,根据视图的宽度将其移动到中心位置。

您可以将您的imageview放置在其他视图组的内部(相对layout布局的线性布局),保留imageview的边距,然后对视图组执行
android:centerInParent=“true”

<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>


这非常有用。非常感谢。居中framelayout并添加填充/如果是这样,则将边距更改为高值将更改图像的位置。但我正在安卓4.2上测试,似乎centerInParent并没有完全覆盖相对布局中的任何空白。