我已经用Java实现了AdMob添加,并且不知道如何修复一件事。当我的添加出现时,我的视图位于相同的位置,因此添加在顶部并覆盖一些东西。你知道怎么修这个吗?
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>
答案 0 :(得分:1)
将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);
<强>更新强>
在LinearLayout
textView之前向您的布局添加tvNumber
,其中包含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's
可见性,如下所示......
if(checkInternetConnection()==true){
layout.setVisibility(View.VISIBLE);
} else {
layout.setVisibility(View.GONE);
}
将此条件置于需要控制layout
的可见性的位置。