2011-05-12 115 views
0

我正在嘗試製作可點擊的圖片,但圖片上有許多可點擊的點。 我認爲最好的辦法是創建一組視圖,並保持在圖像上方並且可點擊。如何查看圖像?

但是我很難將它們放在正確的位置上。

有沒有人有如何做到這一點的想法?

+0

如果可能,發佈您的佈局xml代碼 – FoamyGuy 2011-05-12 15:52:29

回答

0

能代替你做這樣的事情

imageView.setOnTouchListener(new View.OnTouchListener() 
{ 
    @Override 
    public boolean onTouch(View v, MotionEvent event) 
    { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) 
    { 
     // event.getX() and event.getY() give you the co-ordinate 
     // of the touch, and from that you can work out which thing 
     // has been touched and so what to do? 
    } 
    } 
}); 


甲笨RelativeLayout的例子,位置含有一個圖標在一個TextView包含大量文字中的三項imageviews:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:id="@+id/textView1" android:text="TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView TextView " android:layout_alignParentTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
    <ImageView android:id="@+id/imageView1" android:src="@drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true"></ImageView> 
    <ImageView android:id="@+id/imageView2" android:src="@drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="10dp"></ImageView> 
    <ImageView android:id="@+id/imageView3" android:src="@drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="25dp" android:layout_marginTop="35dp"></ImageView>  
</RelativeLayout> 

注意,z順序由順序在XML定義,以便進一步在文件中的東西是,它在顯示器中的位置越高。

+0

確實我可以,我試圖避免這種情況,因爲我沒有形象和這種方式將導致更多的代碼,但是,如果沒有辦法,它將不得不做。 我問,因爲之前有一個AbsoluteLayout,而且這正是我正在尋找的東西。 – Gust 2011-05-12 16:14:17

+0

嘗試使用RelativeLayout,將您的東西設置爲與圖像相同的頂部/左側對齊,然後使用填充/邊距將它們移動到正確的位置? – 2011-05-12 16:31:38

+0

(如果評論不明確,在原始答案中增加了xml示例。) – 2011-05-12 16:43:35

0

你需要考慮的FrameLayout和Z順序像這個問題的答案here

希望這是你在找什麼!

+0

我試圖使用FrameLayout,但我只能看到一個視圖。 我想我需要填充它,所以它不會隱藏下面的,我是tryong:'pc1.setPadding(100,100,0,0); t.addView(pc2,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); t.addView(pc1,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));' 到目前爲止沒有工作:) – Gust 2011-05-12 16:01:58

0

應用實現OnClickListener接口到它

ImageView的ImageView的=(ImageView的)findViewById(R.id.imageView);

imageView.setOnClickListener(新OnClickListener()

{恥骨無效的onClick(查看爲arg0){

//你的代碼在這裏

} 

});