2015-03-02 56 views
0

我有一些區域的圖像。我想對特定區域的點擊作出反應,但我無法將此圖像剪切成單獨的圖像。你可以看到下面的例子。Android - 如何佈局圖像中的按鈕?

Example image

,圓點是我的領域。紅色矩形是我的透明按鈕。你將如何在XML中實現這一點?我想確保活動按鈕將始終覆蓋背景圖像中的相同區域。

我試圖做到這一點使用的FrameLayout:

<FrameLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <ImageView 
       android:id="@+id/imageView1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:scaleType="fitCenter" 
       android:src="@drawable/_podklad" /> 
      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
       <ImageButton 
        android:layout_gravity="right|bottom" 
        android:contentDescription="prawy_tyl" 
        android:layout_marginRight="45dp" 
        android:layout_marginBottom="40dp" 
        android:layout_width="65dp" 
        android:layout_height="75dp" 
        android:background="@null" 
        android:src="@null"/> 

       <ImageButton 
        android:contentDescription="lewe_kolo_przod" 
        android:layout_marginLeft="30dp" 
        android:layout_marginTop="38dp" 
        android:layout_width="50dp" 
        android:layout_height="50dp" 
        android:background="@null" 
        android:src="@null"/> 
       <ImageButton 
        android:layout_gravity="bottom" 
        android:contentDescription="lewe_kolo_tyl" 
        android:layout_marginLeft="28dp" 
        android:layout_marginBottom="75dp" 
        android:layout_width="50dp" 
        android:layout_height="50dp" 
        android:background="@null" 
        android:src="@null"/> 
       <ImageButton 
        android:layout_gravity="right" 
        android:contentDescription="prawe_kolo_przod" 
        android:layout_marginRight="28dp" 
        android:layout_marginTop="40dp" 
        android:layout_width="50dp" 
        android:layout_height="50dp" 
        android:background="@null" 
        android:src="@null"/> 
       <ImageButton 
        android:layout_gravity="right|bottom" 
        android:contentDescription="prawe_kolo_tyl" 
        android:layout_marginRight="25dp" 
        android:layout_marginBottom="75dp" 
        android:layout_width="50dp" 
        android:layout_height="50dp" 
        android:background="@null" 
        android:src="@null"/> 
      </RelativeLayout></FrameLayout> 

正如你可以看到我試圖指定的按鈕固定的寬度,高度和利潤,但在Android的工作室,它的工作以及在Nexus 4預覽,但在像Nexus 9這樣的大屏幕上,這些按鈕放錯了位置。

我等待你的想法。

問候。

回答

3

,因爲你知道你的形象,你知道位於該圖像中的像素,我不建議使用的透明按鈕 您可以改用(onTouchListener)這個圖像,將響應這種觸控先做View.measure的地區此圖像,然後根據(x,y)軸進行映射

+1

我已經實現它使用'onTouchListener',如你所說:)現在我不需要擔心按鈕錯位。謝謝你的啓發! :) – 2015-03-03 10:27:24

+0

不客氣:) – 2015-03-03 11:11:10

0

在圖像中添加圓,並更改padding = 10dp或padding 20dp的屬性。 或填充左或填充 - 右