RelativeLayout - 居中并在父容器中央和marginTop

12

我有以下的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被忽略了。

5个回答

28
如果您希望第二张图像在屏幕中心下方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居中并添加填充。 - Lazy Ninja

9

使用center in parent时,视图会直接放置在中心。如果一个对象距离视图顶部不到90像素,margin top就只会起到作用。因此,在保留其顶部至少90像素的空间的情况下,将居中的视图向下推。因此,它并不被忽略,但它并没有产生您认为应该有的效果。


如果是这样的话,将边距更改为高值将更改图像的位置。但我正在Android 4.2上进行测试,似乎centerInParent完全覆盖了相对布局中的任何边距。 - Behnam

3

我希望进度条显示在android:layout_centerInParent="true"下方,因此我添加了一个虚拟的TextView并将其设置为centerInParent。然后我把我的进度条放在它下面。现在你可以通过两种方式增加它与中心的距离。第一种是增加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>

1
你可以将 ImageView 放在其他 ViewGroup(如 LinearLayout 或 RelativeLayout 布局)中,保留 ImageView 的 margin,并对 ViewGroup 进行 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>

0
你可以创建一个虚拟视图并将其居中到父视图。现在使用layout:alignComponent相对于虚拟视图对齐你的视图,并给它marginTop属性。现在在代码中按照你的视图宽度移动它以使其居中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接