1

我正在开发一个应用程序。需要的是,我想创建一个如图所示的布局:

布局

在此,我采用了一个相对布局,我将图像作为背景。图像没有红点(按钮)。

我将红色按钮作为图像按钮放置在固定位置。当我在我的 Galaxy 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 Optimus L3 E400 中,它看起来像:

布局 2

红色按钮不在位置上。我不想为此使用地图视图,因为我不知道并且从未使用过它。

请指导我如何摆脱这个问题。至于如何为最大分辨率和屏幕支持创建布局。

4

2 回答 2

3

最简单的方法是创建一个自定义视图,其中包含图像(位图)及其顶部的按钮,并使用onDraw方法绘制按钮,并分析坐标onTouch以创建触摸事件。

如果您保持图像的纵横比(宽度:高度),您可以使用缩放轻松地将按钮映射到合适的位置。

以下是自定义视图示例的列表:

你也可以使用AndEngine安卓的 2D 图形/游戏引擎,它会为你处理缩放。

看这里:官方网站例子

于 2013-05-03T10:56:39.357 回答
2

This can be done best with Canvas.

Or the easier way to do this would be edit the image and add those red dots in image itself.

you can use onTouchListener and in onTouch you can get the x and y coordinate to accomplish your task

Something like this:-

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

This is'nt the best way to handle it. But should work.

于 2013-05-03T10:50:57.080 回答