2013-05-03 88 views
1

我正在開發一個應用程序。我們需要的是因爲,我想創建一個佈局如圖所示:創建圖像佈局

Layout

在此,我參加了一個相對佈局,而我給了一個圖片作爲背景。 圖像沒有紅點(按鈕)。

我將紅色按鈕作爲圖像按鈕放置在固定位置。當我在銀河s2上運行它時,它看起來很好,工作也很好。

但是當我在LG Optimus L3 E400上測試了這個佈局。我設定的紅色按鈕不在他們的位置上。

佈局的代碼是:

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

    <TextView 
     android:id="@+id/textview_navigationbar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/navigationbar" 
     android:text="@string/string_map" 
     android:gravity="center" 
     android:textIsSelectable="false" /> 


    <RelativeLayout 
     android:layout_below="@+id/textview_navigationbar" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginTop="50sp" 
     android:layout_marginBottom="50sp" 
     android:background="@drawable/map"> 

     <ImageButton 
      android:id="@+id/imagebutton_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="100dip" 
      android:layout_marginLeft="50dip" 
      android:background="@drawable/button_1" /> 

     <ImageButton 
      android:id="@+id/imagebutton_2" 
      android:layout_below="@+id/imagebutton_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="30dip" 
      android:layout_marginLeft="6dip" 
      android:background="@drawable/button_2" />   

     <ImageButton 
      android:id="@+id/imagebutton_3" 
      android:layout_below="@+id/imagebutton_2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="1dip" 
      android:layout_marginLeft="40dip" 
      android:background="@drawable/button_3" /> 

    </RelativeLayout> 

</RelativeLayout> 

在我的LG擎天柱L3 E400,它看起來像:

Layout 2

紅色按鈕不在位置。 我不想使用地圖視圖,因爲我不知道,也從來沒有使用它。

請指導我如何擺脫這個問題。作爲如何創建最大分辨率和屏幕支持的佈局。

+1

它不會是相同的。由於設備尺寸是不同的,因此將獲得在該位置有所不同.. – itsrajesh4uguys 2013-05-03 10:45:05

+0

@ itsrajesh4uguys:那麼如何在來的吧。 – 2013-05-03 10:47:03

+0

你需要使用畫布或類似的東西來繪畫... – itsrajesh4uguys 2013-05-03 10:57:05

回答

2

這可以用Canvas最好地完成。

或者更簡單的方法是編輯圖像並在圖像本身中添加這些紅點。

可以使用onTouchListener和onTouch你可以得到x和y座標來完成你的任務是這樣

東西: -

public boolean onTouch(View v, MotionEvent event) { 
     int x = event.getX(); 
     int y = event.getY(); 
     //handle your events on basis of x and y 
    } 

這is'nt來處理它的最佳方式。但應該工作。

+0

如果我在圖像本身添加紅色按鈕,那麼我怎麼點擊它。 – 2013-05-03 10:52:39

3

最簡單的方法是創建一個自定義視圖來保存圖像(位圖)及其上的按鈕,並使用onDraw方法繪製按鈕並分析onTouch上的座標以創建觸摸事件。

如果您保持圖像的縱橫比(寬度:高度),則可以使用縮放比例輕鬆將按鈕映射到合適的位置。

這裏是一個自定義視圖示例的列表:

您還可以使用AndEngine一個2D圖形/ Android的遊戲引擎,它會照顧你的縮放。

到這裏看看:official websiteexamples