2012-10-28 32 views
6

Tôi có bố cục Tương đối bên trong với 2 hình ảnh và khi tôi đặt thuộc tính android: layout_marginRight , nó không hoạt động.android: layout_marginTính năng thuộc tính không hoạt động trên RelativeLayout

Đây là mã của tôi:

<RelativeLayout 
    android:id="@+id/location_image_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" > 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="80dp" /> 

    <ImageView 
     android:layout_width="69.33dp" 
     android:layout_height="72dp" 
     android:background="@drawable/icon" 
     android:layout_centerInParent="true" /> 

</RelativeLayout> 
+0

bạn đang cố gắng đạt được điều gì? – wtsang02

+0

Tôi muốn hình ảnh trên cùng nằm ở bên trái của hình ảnh thứ hai và căn chỉnh hình ảnh ở phía dưới cùng của hình ảnh thứ hai. – meh

+0

Có rất nhiều câu trả lời hay. Đó là một thực hành tốt để upvote phản ứng mà bạn thích. – Siddharth

Trả lời

21

Sự cố nằm trong thuộc tính của bố cục android:layout_width. Khi được đặt thành "wrap_content", android:layout_marginRight sẽ không hoạt động, nhưng thay vào đó, chỉ khi được đặt thành "fill_parent" thì android:layout_marginRight sẽ hoạt động.

+0

vào năm 2017: bạn có thể sử dụng android: layout_marginEnd if API> = 17. Và vì API 8 chúng ta nên sử dụng "match_parent" chứ không phải "fill_parent". – PedroHidalgo

0

Bạn đã sử dụng thats sau do tại sao nó đang xảy ra

android:layout_alignParentRight="true" 

chỉ cần loại bỏ dòng này bạn sẽ nhận được lợi nhuận từ quyền trong ImageView của bạn ..

+0

Tôi cần bố cục đó để căn chỉnh ở bên phải. – meh

1
<LinearLayout 
    android:id="@+id/location_image_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="right" 
    android:orientation="vertical" > 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="80dp" /> 

    <ImageView 
     android:layout_width="69.33dp" 
     android:layout_height="72dp" 
     android:background="@drawable/icone" 
     android:layout_centerInParent="true" /> 

</LinearLayout> 

Hãy thử điều này. Nó làm việc cho tôi và chính xác rằng bạn muốn

-4

Hãy thử sử dụng LinerarLayout thay vì RelativeLayout để áp dụng Ký quỹ trong Bố cục của bạn.

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

Tôi gặp phải vấn đề tương tự và nếu bạn thay đổi bố cục RelativeLayout của bố cục thành "match_parent" thì nó hoạt động. Hi vọng nó có ích cho bạn.

Các vấn đề liên quan