我想创建以全屏方式加载图片广告的全屏 Activity。示例图像是这样的:
请注意,按钮也是图像的一部分。这是布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ad_image">
</LinearLayout>
你可以看到图像加载了 android:background
。
我想要实现的是:当“按钮”图像部分被点击时,它会做一些事情(比如启动另一个 Activity ),以及右上角的“关闭 [X]”矩形,当被点击时, 它将关闭 Activity 。
在“按钮”绘图的区域部分实现点击逻辑似乎很容易。到目前为止,我找到了两种方法:
在 LinearLayout 上设置 ontouch 监听器并检查按钮绘图区域的 X 和 Y 坐标
将不可见的
ImageView
放在布局中“Button”图像的顶部,并设置 onclick 监听器。
但问题是:许多 Android 设备有多种屏幕尺寸和分辨率。所以我想知道这两个解决方案是否可以在不同屏幕尺寸上以便携方式使用?
由于在不同的屏幕分辨率下加载图像时图像可能会被拉伸(stretch),这意味着对于解决方案 #1:我必须调整 X 和 Y 数字。对于解决方案 #2:不可见的 ImageView 的位置可能会与 Button 绘图同步。
所以有人知道更好的解决方案吗?
更新:
我正在尝试使用隐形点击占位符解决方案。这是布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ad_image"
android:id="@+id/adLayout"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.75"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:id="@+id/adImageLayout"
android:onClick="adImageClicked">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
adImageLayout
是按钮绘制的点击占位符。
最佳答案
我决定回答我的问题。我改进了我原来的解决方案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ad_image"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.7">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="@drawable/xbutton"
android:onClick="xButtonClicked"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.3"
android:onClick="buttonClicked"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
这是布局的架构:
关闭按钮加载了ImageView。
所以基本上我只是在这里使用 layout_weight
技巧。我避免了硬编码 dpi,因为它会导致不同的 LinearLayout 的宽度/高度导致不同的屏幕尺寸。
我测试了几种屏幕分辨率(平板电脑尺寸除外),效果很好。我不需要为多种密度提供不同的图像和布局文件。而且 Activity 类中所需的代码非常少,只有设置全屏的代码:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
和 onClick
事件方法。
当然,如果您能找到更好的解决方案,请告诉我。
关于java - 如何在android中为多个屏幕尺寸实现图像区域点击监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27752863/