2011-10-25 31 views
10

Tôi có XML sau:RelativeLayout - CenterInParent và 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> 

Nhưng nó trông giống như các marginTop đang được bỏ qua.

Trả lời

8

Khi bạn sử dụng trung tâm trong bố mẹ, chế độ xem được đặt trực tiếp vào chính giữa. Đầu lề sẽ chỉ xuất hiện nếu đối tượng nằm trong phạm vi 90px ở đầu chế độ xem của bạn. Do đó, đẩy chế độ xem trung tâm xuống để giữ ít nhất 90px không gian phía trên nó. Vì vậy, nó không phải là bị bỏ qua, nhưng nó không có tác dụng mà bạn nghĩ rằng nó cần phải có.

+0

Nếu đó là trường hợp, sau đó thay đổi bên lề để một giá trị cao sẽ thay đổi vị trí của hình ảnh. Nhưng tôi đang thử nghiệm trên Android 4.2 và có vẻ như centerInParent hoàn toàn ghi đè bất kỳ lề nào trong bố cục Tương đối. – Behnam

24

Nếu bạn muốn hình ảnh thứ hai là 90dp ở giữa màn hình, bạn có thể thay thế hình ảnh đó bằng FrameLayout, nơi bạn có thể kiểm soát phần đệm để di chuyển hình ảnh của mình xuống dưới.

<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

Điều này rất hữu ích. Cảm ơn bạn. Trung tâm framelayout và thêm padding/ –

+0

đây phải là câu trả lời được chấp nhận –

3

Tôi muốn progressbar để hiển thị dưới android:layout_centerInParent="true" vì vậy tôi đã thêm một hình nộm TextView và đặt nó vào centerInParent .Sau đó tôi đặt progressbar tôi dưới nó. Bây giờ bạn có thể tăng khoảng cách từ trung tâm theo hai cách. Đầu tiên bằng cách tăng marginTop trong TextView và tăng chiều cao TextView thứ hai.

<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

Bạn có thể tạo chế độ xem giả và căn giữa cho bố mẹ. Bây giờ căn chỉnh chế độ xem của bạn liên quan đến chế độ xem giả sử dụng bố cục: alignComponent và cung cấp cho marginTop. Bây giờ trong mã di chuyển nó theo chiều rộng của quan điểm của bạn để trung tâm nó.

1

Bạn có thể đặt ImageView của bạn bên trong của ViewGroup khác (LinearLayout của RelativeLayout bố trí), để lại bên lề ImageView, và làm android:centerInParent="true" cho ViewGroup:

<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> 
Các vấn đề liên quan