RelativeLayout - CenterInParent和marginTop

时间:2011-10-25 20:13:18

标签: 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_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 个答案:

答案 0 :(得分:26)

如果您希望第二张图像在屏幕中央下方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 :(得分:9)

当您在父母中使用中心时,视图将直接放在中心。仅当对象位于视图顶部的90px范围内时,边距顶部才会起作用。因此,向下推中心视图以在其上保持至少90px的空间。所以它不会被忽视,但它没有你认为应该具有的效果。

答案 2 :(得分: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>

答案 3 :(得分:1)

您可以将您的imageview放在其他ViewGroup(RelativeLayout布局的LinearLayout)中,留下imageview的边距,并为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>

答案 4 :(得分:0)

您可以制作虚拟视图并将其居中于父级。现在使用layout:alignComponent将您的视图相对于虚拟视图对齐,并给出marginTop。现在在代码中根据视图的宽度移动它以使其居中。