gpt4 book ai didi

android - Android 中 ScrollView 中的广告对齐方式

转载 作者:行者123 更新时间:2023-11-30 03:40:14 25 4
gpt4 key购买 nike

我正在尝试在 Android 上创建一个带有广告的页面(使用广告暴民)。下面是我正在使用的代码:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >

<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/adView_relative_layout" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Primary Screen sssssssss ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss sssssssssssssssssssssssssssssssss ssssssssssssssssssssssssssssssssssssssssss sssssssssssssssssssssssssssssssssssssssss ssssssssssssss s jkjkljlkjkljsjkjlkjskjkjkl jkljkljkl jkljlkj kljkljkl jkljlkjkljkl jkljlkjkljlkjlk jlkjlkjlkjlkjlkjlkj jkljlkjlkjlkjlkjlkjlkjlkjlk jlkjlkjlkjkljlkjlkj lkjlkjlkjklj kljlkjlkjlkjkljlkjlkjlkjlkjlkjlk lkjlkjlkjlkjlkj jlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkj lkjlkjlk lkjlkjlknigpoihgpoj k lkjcfhkl jiuvlk jkjhu hiuhjlkjliijv kjgjhhgkjjhlkkjk"
android:textSize="20sp" />

<Button
android:id="@+id/SwitchToSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Switch to Second Screen"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>

<RelativeLayout
android:id="@+id/adView_relative_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >

<com.google.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/MY_AD_UNIT_ID"
ads:loadAdOnCreate="true" />
</RelativeLayout>

</RelativeLayout>

但问题是广告出现在屏幕的顶部和底部(这就是我想要的)但是它通过广告部分覆盖了文本顶部和底部的文本(即使我向上或向下滚动)。并且我看不到文字

请指导如何对齐文本和 ScrollView ,以便我可以看到所有文本以及广告 View 。

在下面找到我的 Activity.java 代码:

package com.abc.testapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.view.*;
import android.content.Intent;
import com.google.ads.*;

public class HomeActivity extends Activity {
/** Called when the activity is first created. */
Button screen2Button=null;
//private AdView adView;
private AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_home);
screen2Button=(Button)findViewById(R.id.SwitchToSecond);
screen2Button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent myIntent=new Intent(v.getContext(),SecondScreen.class);
startActivity(myIntent);
}
});
String s1 = getResources().getString(R.string.MY_AD_UNIT_ID);
// Create the adView
adView = new AdView(this, AdSize.BANNER, s1);

// Lookup your LinearLayout assuming it's been given
// the attribute android:id="@+id/mainLayout"
RelativeLayout layout = (RelativeLayout)findViewById(R.id.LinearLayout1);

// Add the adView to it
layout.addView(adView);

// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}

@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}

}

最佳答案

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >

<com.google.ads.AdView
android:id="@+id/adView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
ads:adSize="BANNER"
ads:adUnitId="@string/MY_AD_UNIT_ID"
ads:loadAdOnCreate="true" />

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/adView1"
android:layout_above="@+id/adView2" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Primary Screen sssssssss ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss sssssssssssssssssssssssssssssssss ssssssssssssssssssssssssssssssssssssssssss sssssssssssssssssssssssssssssssssssssssss ssssssssssssss s jkjkljlkjkljsjkjlkjskjkjkl jkljkljkl jkljlkj kljkljkl jkljlkjkljkl jkljlkjkljlkjlk jlkjlkjlkjlkjlkjlkj jkljlkjlkjlkjlkjlkjlkjlkjlk jlkjlkjlkjkljlkjlkj lkjlkjlkjklj kljlkjlkjlkjkljlkjlkjlkjlkjlkjlk lkjlkjlkjlkjlkj jlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkj lkjlkjlk lkjlkjlknigpoihgpoj k lkjcfhkl jiuvlk jkjhu hiuhjlkjliijv kjgjhhgkjjhlkkjk"
android:textSize="20sp" />

<Button
android:id="@+id/SwitchToSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Switch to Second Screen"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>


<com.google.ads.AdView
android:id="@+id/adView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/MY_AD_UNIT_ID"
ads:loadAdOnCreate="true" />

编辑:我认为您应该尝试使用线性布局,因为您想要非常线性的东西(广告,然后是 ScrollView,然后是广告)。我已经编辑了上面的 xml 以反射(reflect)这一点。虽然它未经测试,因此您可能需要稍微调整一下。

修改3:在ScrollView上方添加第二个AdView,满足顶部一个AdView,底部一个AdView的需求。改回 RelativeLayout,这样一个 AdView 可以在顶部对齐,一个在底部对齐。

关于android - Android 中 ScrollView 中的广告对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15866276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com