Android - 如何在图像中布置按钮?
Android - How to layout buttons inside 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 studio 中,它在 Nexus 4 预览中运行良好,但在 Nexus 9 等更大的屏幕上,按钮放错了位置。
我等你的想法。
你好。
因为你知道你的图像并且你知道图像中的区域以像素为单位我不建议使用透明按钮
您可以改为使用 (onTouchListener
) 作为此图像的响应,首先对此图像执行 View.measure
,然后根据 (x,y) 轴
进行映射
在图片中添加圆圈,并更改padding = 10dp 或padding 20dp 的属性。
或 padding-left 或 padding-right
我有一些带区域的图像。我想对特定区域的点击做出反应,但我无法将此图像切割成单独的图像。你可以看下面的例子。
黑圈是我的领域。红色矩形是我的透明按钮。你将如何在 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 studio 中,它在 Nexus 4 预览中运行良好,但在 Nexus 9 等更大的屏幕上,按钮放错了位置。
我等你的想法。
你好。
因为你知道你的图像并且你知道图像中的区域以像素为单位我不建议使用透明按钮
您可以改为使用 (onTouchListener
) 作为此图像的响应,首先对此图像执行 View.measure
,然后根据 (x,y) 轴
在图片中添加圆圈,并更改padding = 10dp 或padding 20dp 的属性。 或 padding-left 或 padding-right