在开发Android应用时,有时为了收益会增加一些广告。但是广告不断的滚动显示,又很烦人,降低用户体验,所以就需要增加一个关闭的按钮来隐藏或者关闭广告。有的应用是直接将关闭广告的功能做到了设置界面里边,但有的是在广告上增加了一个按钮。这里以万普的迷你广告为例。
万普的迷你广告是一个LinearLayout,我开始尝试着在上面直接添加一个关闭按钮,然后设置相应的布局参数,但是效果不理想。后来改用RelativeLayout作为父容器,将迷你广告的LinearLayout和关闭按钮一同放进去。下面是实现的代码。
Waps.java
package com.sparkle.ad; import android.app.Activity; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.graphics.Color; import android.opengl.Visibility; import android.text.Layout; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.RelativeLayout; import cn.waps.AppConnect; import com.sparkle.qrcode.R; public class Waps { private static String _appId = ""; private static String _advertismentPlatform = "default"; private static AppConnect _appConnect = null; private static void GetChannelInfo(Activity activity) { ApplicationInfo appInfo = null; try { appInfo = activity.getPackageManager().getApplicationInfo( activity.getPackageName(), PackageManager.GET_META_DATA); } catch (NameNotFoundException e) { e.printStackTrace(); } _appId = appInfo.metaData.getString("APP_ID"); _advertismentPlatform = appInfo.metaData.getString("APP_PID"); } public static void Start(Activity activity) { GetChannelInfo(activity); _appConnect = AppConnect.getInstance(_appId, _advertismentPlatform, activity); if (_appConnect == null) { return; } _appConnect.setAdBackColor(Color.argb(50, 120, 240, 120)); _appConnect.setAdForeColor(Color.YELLOW); LinearLayout miniLayout = GenerateMiniLinearLayout(activity); _appConnect.showMiniAd(activity, miniLayout, 10); } private static LinearLayout GenerateMiniLinearLayout(Activity activity) { final RelativeLayout relativeLayout = new RelativeLayout(activity); LinearLayout miniLayout = new LinearLayout(activity); ImageView close_imageView = new ImageView(activity); close_imageView.setBackgroundResource(R.drawable.close); close_imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { relativeLayout.setVisibility(View.GONE); } }); RelativeLayout.LayoutParams closeParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); closeParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); close_imageView.setLayoutParams(closeParams); relativeLayout.addView(miniLayout); relativeLayout.addView(close_imageView); FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); layoutParams.gravity=Gravity.BOTTOM; activity.addContentView(relativeLayout, layoutParams); return miniLayout; } public static void Close(Context context) { AppConnect.getInstance(context).close(); } }注:
(1)、APP_ID和APP_PID在AndroidManifest.xml中配置。
(2)、Start函数用来启动广告。
(3)、Close函数用来关闭广告。
(4)、GenerateMiniLinearLayout函数中生成了RelativeLayout的相对布局。在RelativeLayout中,添入迷你广告miniLayout和关闭按钮close_imageView。close_imageView设置背景图和点击事件。点击事件中,只是将广告隐藏了,如果有需要可以直接关闭。
要点:
(1)、为了能将close_imageView设置到右侧,需要使用RelativeLayout.LayoutParams(不是LinearLayout.LayoutParams,我开始用的是这个,然后用gravity,怎么都达不到效果),再使用addRuler来设置ALIGN_PARENT_RIGHT。
(2)、在addContentView中,需要布局RelativeLayout。使用FrameLayout.LayoutParams来布局(注意一定要用这个,RelativeLayout.LayoutParams和LinearLayout.LayoutParams都用过,达不到效果),设置gravity即可。
下面是效果图:
【我所认知的BIOS】—> uEFI AHCI Driver(2) — 原来开始也不那么简单,布布扣,bubuko.com