2012-08-14 41 views
14

Đây là cách bố trí của tôi mà tôi đã cố gắng cho đến nay không có bất kỳ thành côngLàm thế nào để đặt một ImageView trên đầu trang của một ImageView trong android

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@color/white"> 

<LinearLayout 
    android:id="@+id/lltest" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_centerHorizontal="true"> 

     <ImageView 
     android:id="@+id/inside_imageview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip" 
     android:layout_marginBottom="5dip" 
     android:src="@drawable/frame"/> 

</LinearLayout> 

<ImageView 
     android:id="@+id/outside_imageview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@id/inside_imageview" 
     android:scaleType="fitXY"/> 
</RelativeLayout> 

gì tôi chính xác muốn là phải có outside_imageview của tôi trên đầu trang của inside_imageview với chính xác chiều cao và chiều rộng ... Làm thế nào để làm điều đó thông qua bố trí?

+0

bạn đã thử thêm layout_alignLeft = "@ id/inside_imageview "ngoài alignTop? – CSmith

+0

@CSmith Có, tôi đã thử. –

+0

Vấn đề là anh ta có inner_imageView bên trong một LinearLayout vì vậy thuộc tính 'layout_alignTop' không thể thấy ID tham chiếu đến' inner_imageView'. 'external_imageview' sẽ căn chỉnh với LinearLayout nếu tham chiếu ID là' lltest' thay thế. – DeeV

Trả lời

34
<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@color/white" > 

    <ImageView 
     android:id="@+id/inside_imageview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="5dip" 
     android:layout_marginTop="5dip" 
     android:src="@drawable/frame" /> 

     <ImageView 
     android:id="@+id/outside_imageview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@id/inside_imageview" 
     android:layout_alignBottom="@id/inside_imageview" 
     android:layout_alignLeft="@id/inside_imageview" 
     android:layout_alignRight="@id/inside_imageview"    
     android:scaleType="fitXY" /> 
    </RelativeLayout> 

Các layout_align[Top|Bottom|Left|Right] thuộc tính trong RelativeLayout được sử dụng để sắp xếp khung nhìn dựa trên x tương ứng của họ và giá trị y trong lề. Thứ hai ImageView bây giờ sẽ được căn chỉnh ở trên cùng, dưới cùng, bên trái và bên phải của ImageView đầu tiên dựa trên lợi nhuận. Đệm được bỏ qua trong căn chỉnh.

+0

Hmmm ... Thực ra tôi có thêm hai lần xem hình ảnh cùng với inner_imageview ... vì vậy tôi nghĩ đến việc đặt chúng trong một linearlayout riêng biệt ... Cái bên trong tôi cần phải làm một định hướng ngang và bên ngoài tôi muốn một chiều dọc ... Vì vậy, xin đề nghị tôi trong này –

+0

Điều đó làm phức tạp thêm một chút. Bạn không thể tham chiếu lượt xem là trẻ em trong các chế độ xem khác. Họ chỉ chấp nhận tài liệu tham khảo cho trẻ em ở cùng cấp độ. Nếu 'inner_imageview' và' external_imageview' có chiều rộng và chiều cao giống nhau, thì bạn chỉ cần đặt cả hai trong hai 'Linearlayouts' riêng biệt và căn chỉnh hai' LinearLayouts' ở trên cùng và bên trái. Nếu kích thước khác nhau, thì bạn có thể phải sử dụng 'layout_below' cho một và' layout_toRightOf' cho thứ nguyên kia. – DeeV

+0

câu trả lời tuyệt vời! yêu nó ! – uLYsseus

6

Bạn nên thử FrameLayout. Bên trong một FrameLayout mọi Child nằm trên đầu trang của nhau.

+0

chỉ 1 hình ảnh đang được hiển thị trên thiết bị và 2 hình ảnh, một hình ảnh trên đầu kia đang được hiển thị chính xác trong chế độ xem thiết kế bố cục – iOSAndroidWindowsMobileAppsDev

4

Bạn cần bố cục khung. Thử kiểm tra AndroidFrameLayout.

+0

chỉ 1 hình ảnh đang được hiển thị trên thiết bị và 2 hình ảnh, một hình ảnh trên đầu kia đang được hiển thị chính xác trong chế độ xem thiết kế của bố cục – iOSAndroidWindowsMobileAppsDev

19

FrameLayout là những gì bạn cần. Bạn chỉ có thể hợp nhất bố cục gốc là FrameLayout. Hãy nhìn vào các phát triển Android Blog: http://android-developers.blogspot.it/2009/03/android-layout-tricks-3-optimize-by.html

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center"> 

    <ImageView 
     android:id="@+id/outside_imageview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scaleType="fitXY"/> 

    <ImageView 
     android:id="@+id/inside_imageview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dip" 
     android:layout_marginBottom="5dip" 
     android:src="@drawable/frame" /> 
</merge> 
+2

đáng buồn là người đã đặt câu hỏi, không hiểu và thậm chí không muốn để hiểu bố cục khung nào có thể làm và downvoted tất cả chúng ta. upvoted u two guys :) –

+0

Điều này thậm chí làm giảm hệ thống phân cấp xem theo một cấp: woot: –

+0

Giải pháp tốt. FrameLayout chắc chắn là con đường để đi. – nurxyz

0

tôi đã cố gắng theo cách này và nó làm việc cho tôi, hy vọng nó sẽ giúp

<FrameLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="5dip"> 
<ImageView 
    android:id="@+id/image_first" 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/imga"/> 
<ImageView 
    android:id="@+id/imageview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/image"/> 
</FrameLayout> 
+0

đang được hiển thị trên thiết bị và 2 hình ảnh, một ảnh trên đầu kia là được hiển thị chính xác trong chế độ xem thiết kế của bố cục – iOSAndroidWindowsMobileAppsDev

+0

do đó, hãy giải quyết vấn đề –

1
   <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:padding="@dimen/_15dp"> 
       <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:padding="@dimen/_10dp" 
        android:layout_marginTop="@dimen/_50dp" 
        android:background="@drawable/white_background" 
        /> 
       <ImageView 
        android:layout_width="@dimen/_100dp" 
        android:layout_height="@dimen/_100dp" 
        android:background="@drawable/my_credit" 
        android:layout_marginTop="@dimen/_5dp" 
        android:layout_centerHorizontal="true" 
        /> 
      </RelativeLayout> 
+0

Bạn có thể giải thích chính xác mã của mình không? – Alex

+0

nó đặt một số lần xem hình ảnh ở trên một hình ảnh khác ... không cần sử dụng bất kỳ bố cục khung nào – Sunil

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