gpt4 book ai didi

java - AdMob 覆盖了我的 EditText 和 TextView

转载 作者:行者123 更新时间:2023-12-01 13:18:16 25 4
gpt4 key购买 nike

我已经在 J​​ava 中实现了 AdMob 添加,但不知道如何解决一件事。当我的添加出现时,我的 View 处于相同的位置,因此添加位于顶部并覆盖很少的内容。你知道如何修复这个吗?

    adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layout.addView(adView, params);


AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();


adView.loadAd(adRequest);

我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:weightSum="200"
android:id="@+id/relLayout"
tools:context="com.converter_numeralsystem.app.MainActivity">
<TextView
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginTop="10dp"
android:text="@string/number"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvNumber" />

<TextView
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginTop="20dp"
android:layout_below="@id/tvNumber"
android:text="@string/result"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvResult" />

<EditText
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:id="@+id/etNumber"
android:layout_marginLeft="5dp"
android:hint="@string/enter_numb"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/tvNumber"
android:layout_alignBottom="@id/tvNumber"/>
<EditText
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_weight="25"
android:id="@+id/etResult"
android:layout_marginLeft="5dp"
android:hint="@string/et_result"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/tvNumber"
android:layout_alignBottom="@id/tvResult"/>
<LinearLayout
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="100"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearButtons"
android:layout_below="@id/etResult">

<Button
android:layout_weight="30"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/calculate"
android:id="@+id/bCalculate"
android:layout_toLeftOf="@+id/bReset" />

<Button
android:layout_weight="63"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/reset"
android:id="@+id/bReset"
android:layout_alignBottom="@+id/bCalculate" />

</LinearLayout>
<LinearLayout
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linearButtons"
android:layout_weight="15"
android:id="@+id/topLinear">
<ScrollView
android:layout_below="@id/linearButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scroll">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/from"
android:id="@+id/tvFrom"
android:layout_below="@id/tvResult"
android:layout_marginTop="5dp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tvFrom"
android:id="@+id/rgFrom"
android:layout_marginTop="5dp">
<RadioButton

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r2From"
android:text="@string/dwa"/>


<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r8From"
android:text="@string/osiem"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="75dp" />

<RadioButton
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r10From"
android:text="@string/dziesiec"
android:layout_below="@id/r2From"
android:layout_alignStart="@id/r2From" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r16From"
android:text="@string/szesnascie"
android:layout_alignParentRight="true"
android:layout_below="@id/r8From"
android:layout_alignStart="@id/r8From"/>
</RadioGroup>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/to"
android:id="@+id/tvTo"
android:layout_below="@id/rgFrom"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="5dp" />

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTo"
android:layout_marginTop="5dp"
android:id="@+id/rgTO">

<RadioButton
android:checked="true"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r2TO"
android:text="@string/dwa"/>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r8TO"
android:text="@string/osiem"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="75dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r10TO"

android:text="@string/dziesiec"
android:layout_below="@id/r2TO"
android:layout_alignStart="@id/r2TO" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/r16TO"

android:text="@string/szesnascie"
android:layout_alignParentRight="true"
android:layout_below="@id/r8TO"
android:layout_alignStart="@id/r8TO"/>
</RadioGroup>
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>

最佳答案

RelativeLayout.ALIGN_PARENT_TOP 规则添加到 params...

params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

所以,你的代码将是...

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relLayout);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

layout.addView(adView, params);

更新:

tvNumber textView 之前添加一个 LinearLayout 到您的布局,其中将包含如下的 AdView 并添加 android:layout_below="@+id/ad_layout" 作为 tvNumber TextView 的属性...

    <LinearLayout
android:id="@+id/ad_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" >
</LinearLayout>

因此,更新后的 XML 布局将如下...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="@dimen/activity_vertical_margin"
android:weightSum="200"
tools:context="com.converter_numeralsystem.app.MainActivity" >

<LinearLayout
android:id="@+id/ad_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical" >
</LinearLayout>

<TextView
android:id="@+id/tvNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ad_layout"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/number"
android:textSize="20sp" />

<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvNumber"
android:layout_marginTop="20dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="@string/result"
android:textSize="20sp" />

<EditText
android:id="@+id/etNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/tvNumber"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@id/tvNumber"
android:hint="@string/enter_numb"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:singleLine="true" />

<EditText
android:id="@+id/etResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/tvResult"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@id/tvNumber"
android:layout_weight="25"
android:hint="@string/et_result"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:singleLine="true" />

<LinearLayout
android:id="@+id/linearButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etResult"
android:layout_marginTop="5dp"
android:layout_weight="100"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp" >

<Button
android:id="@+id/bCalculate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/bReset"
android:layout_weight="30"
android:text="@string/calculate" />

<Button
android:id="@+id/bReset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/bCalculate"
android:layout_weight="63"
android:text="@string/reset" />
</LinearLayout>

<LinearLayout
android:id="@+id/topLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linearButtons"
android:layout_weight="15"
android:paddingLeft="5dp"
android:paddingRight="5dp" >

<ScrollView
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linearButtons" >

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

<TextView
android:id="@+id/tvFrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvResult"
android:layout_marginTop="5dp"
android:text="@string/from"
android:textSize="20sp" />

<RadioGroup
android:id="@+id/rgFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tvFrom"
android:layout_marginTop="5dp" >

<RadioButton
android:id="@+id/r2From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dwa" />

<RadioButton
android:id="@+id/r8From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="75dp"
android:text="@string/osiem" />

<RadioButton
android:id="@+id/r10From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@id/r2From"
android:layout_below="@id/r2From"
android:checked="true"
android:text="@string/dziesiec" />

<RadioButton
android:id="@+id/r16From"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignStart="@id/r8From"
android:layout_below="@id/r8From"
android:text="@string/szesnascie" />
</RadioGroup>

<TextView
android:id="@+id/tvTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/rgFrom"
android:layout_marginTop="5dp"
android:text="@string/to"
android:textSize="20sp" />

<RadioGroup
android:id="@+id/rgTO"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTo"
android:layout_marginTop="5dp" >

<RadioButton
android:id="@+id/r2TO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/dwa" />

<RadioButton
android:id="@+id/r8TO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="75dp"
android:text="@string/osiem" />

<RadioButton
android:id="@+id/r10TO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@id/r2TO"
android:layout_below="@id/r2TO"
android:text="@string/dziesiec" />

<RadioButton
android:id="@+id/r16TO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignStart="@id/r8TO"
android:layout_below="@id/r8TO"
android:text="@string/szesnascie" />
</RadioGroup>
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>

现在,更改您的代码如下...

LinearLayout layout = (LinearLayout) findViewById(R.id.ad_layout);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

layout.addView(adView, params);

另一个更新:

在类级别声明layout并更改layout的可见性,如下所示...

if(checkInternetConnection()==true){

layout.setVisibility(View.VISIBLE);

} else {

layout.setVisibility(View.GONE);

}

将此条件放置在需要控制布局可见性的位置。

关于java - AdMob 覆盖了我的 EditText 和 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22284618/

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