2013-05-03 89 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