2013-01-08 28 views
12

Tôi muốn tạo chế độ xem tùy chỉnh như thế này. enter image description hereLàm thế nào để đặt các nút trên hình ảnh trong Android?

Tôi đã thử các sau đây

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/customView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:src="@drawable/sample_image" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right|top" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right|bottom" 
     android:text="Button" /> 

</FrameLayout> 

Làm thế nào tôi có thể tạo ra một cái nhìn như thế này? Làm thế nào tôi có thể đặt các nút trên hình ảnh như thế này?

Cảm ơn trước Advance

+0

sử dụng tương đối bố trí. – SKK

+0

kiểm tra câu trả lời của tôi bên dưới. Tôi đã đính kèm một ảnh chụp màn hình. – SKK

Trả lời

14

bạn có thể thử sử dụng bố cục tương đối để thực hiện việc này,

cho btn1;

android:layout_alignParentTop="true" 
android:layout_alignParentRight="true" 

cho btn1;

android:layout_alignParentBottom="true" 
android:layout_alignParentRight="true" 

[EDIT 1]

để cung cấp cho không gian cho lề sử dụng nút, android: layout_margin = "20dp"

enter image description here

Ví dụ bố trí

<RelativeLayout android:layout_width="300dp" 
    android:layout_height="300dp"> 


    <ImageView 
     android:id="@+id/img" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#ffaadd" 
     android:layout_margin="20dp" /> 


    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:text="btn1" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true"  
     android:text="btn2" /> 

</RelativeLayout> 
+0

Tôi muốn hiển thị một số phần của nút ra khỏi imageview.Xin lưu ý hình ảnh được thêm vào. –

+0

Vui lòng xem câu trả lời đã chỉnh sửa – Talha

+0

Thanks.this đã làm việc cho tôi. –

1

Sử dụng RelativeLayout. Điều này sẽ cho phép bạn có các Chế độ xem khác nhau trùng lặp trên màn hình.

1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/RelativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center" 
android:orientation="horizontal" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_weight="1" 
    android:padding="60dp" 
    android:src="@drawable/abs__ab_stacked_solid_dark_holo" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/button1" 
    android:layout_alignBottom="@+id/button1" 
    android:layout_alignParentLeft="true" 
    android:layout_marginLeft="30dp" 
    android:background="@drawable/ic_launcher" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginRight="32dp" 
    android:layout_marginTop="102dp" 
    android:background="@drawable/ic_launcher" /> 
</RelativeLayout> 

snapshot

0

này giải quyết cho tôi

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/img" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src = "@drawable/basic" 
     android:layout_alignParentTop="true" 
     android:layout_alignRight="@+id/button1" 
     android:layout_alignEnd="@+id/button1" /> 


    <ImageButton 
     android:id="@+id/button1" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:text="btn1" 
     android:background="@drawable/ic_cancel_black"/> 



</RelativeLayout> 
Các vấn đề liên quan