在我们开发APP的过程中,难免会需要添加几个广告位,但怎样的广告位才算合理,还需要大家的共同推荐,这里我先介绍几个。
第一种
实现步骤:
1.方法
private View adverView;
//在主线程中调用该方法。
private void loadYMAds() {
// 实例化 LayoutParams(重要)
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER; //位置
adverView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.advertisement, null);
LinearLayout download = (LinearLayout) adverView.findViewById(R.id.download);
ImageView shut = (ImageView) adverView.findViewById(R.id.shut);
shut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
adverView.setVisibility(View.GONE);
}
});
download.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
}
});
this.addContentView(adverView, layoutParams);
}
2.布局
//图片自己改改
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/transparent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/download"
android:layout_width="170dp"
android:layout_height="250dp"
android:background="@drawable/main_first"
android:gravity="center_horizontal"
android:orientation="vertical" >
</LinearLayout>
<ImageView
android:id="@+id/shut"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="40dp"
android:src="@drawable/shut" />
</LinearLayout>
第二种
换个布局,然后改改上面方法中,布局的位置就OK了