【发布时间】:2015-03-01 10:00:09
【问题描述】:
我想创建全屏加载图片广告的全屏活动。示例图像是这样的:
请注意,按钮也是图像的一部分。这是布局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。
我想要实现的是:当点击“按钮”图像部分时,它会做一些事情(比如启动另一个活动),以及点击右上角的“关闭 [X]”矩形,它将关闭活动。
在“按钮”绘图的区域部分实现点击逻辑似乎很容易。到目前为止,我找到了两种方法:
在 LinearLayout 上设置 ontouch 侦听器,并检查 Button 绘制区域的 X 和 Y 坐标
将不可见的
ImageView放在布局中的“按钮”图片之上,并设置onclick监听器。
但问题是:许多 Android 设备都有多种屏幕尺寸和分辨率。所以我想知道这两种解决方案是否可以在不同的屏幕尺寸上以便携的方式使用?
由于图像在以不同的屏幕分辨率加载时可能会被拉伸,这意味着对于解决方案 #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 是按钮绘制的点击占位符。
【问题讨论】:
-
我会使用带有天蓝色背景的 RelativeLayout,然后添加一个 TextView 和一个按钮(关闭 X 和按钮)。第一个将有一个 9 补丁作为背景,后者是一个可绘制的圆角。非常简单,不像全屏图片那样占用内存。
-
@DerGolem:上面的图片只是一个例子,我将使用的真实图片不是这样的纯色背景图片。
-
哦,我明白了。无论如何,在我看来,覆盖在父级背景技术上的 TextView + Button 比实现一些不必要的自定义逻辑更方便。
-
@DerGolem:不幸的是,事情没那么简单。广告图片可能类似于this。而且我不是艺术家,所以图像将由其他人提供。我只是在做代码逻辑。
-
是的,给我一点时间。这完全是关于设计,而不是代码(我将在布局中创建一个共享的 onClick 侦听器并区分代码中的调用者)。让我先准备来自您的示例的示例图像(这需要一些时间,其余的很容易)。