我添加了导航抽屉,ImageView,Image Button和Admob Banner 但是我不知道为什么我的“图像按钮”不起作用,并且也没有显示广告横幅
MainActivity.java
package com.example.mypc.hitarthapp;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
DrawerLayout dl;
NavigationView nv;
ImageButton btn;
ImageView iv;
AdView av;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this,"ca-app-pub-7900332792984616/4506141248");
dl=findViewById(R.id.dv);
nv=findViewById(R.id.nv);
av=findViewById(R.id.ad);
btn=findViewById(R.id.btn);
iv=findViewById(R.id.img);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"My
msg",Toast.LENGTH_LONG).show();
Intent i=new Intent(MainActivity.this,TaskAct.class);
startActivity(i);
}
});
AdRequest ar=new AdRequest.Builder().build();
av.loadAd(ar);
nv.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
{
item.setChecked(true);
dl.closeDrawers();
return true;
}
});
}
}
acitivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="162dp"
android:layout_height="164dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:background="@drawable/wallet" />
<ImageButton
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="247dp"
android:layout_alignParentBottom="true"
android:background="@drawable/startearn" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
tools:context=".MainActivity"
android:layout_alignParentLeft="true">
<android.support.v7.widget.ContentFrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.ContentFrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/holo_blue_bright"
app:headerLayout="@layout/myheader"
app:menu="@menu/menu">
</android.support.design.widget.NavigationView>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/ad"
android:layout_width="10dp"
android:layout_height="10dp"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-7900332792984616/4506141248">
</com.google.android.gms.ads.AdView>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mypc.hitarthapp">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<meta-data
android:name="com.google.android.gms.ads.ca-app-pub-
7900332792984616/4506141248"
android:value="ca-app-pub-7900332792984616/4506141248" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TaskAct"
android:parentActivityName=".MainActivity">
></activity>
</application>
</manifest>
我的build.gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.mypc.hitarthapp"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
//noinspection GradleCompatible
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-ads:15.0.1'
//noinspection GradleCompatible
implementation 'com.android.support:design:27.1.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.2'
implementation 'com.google.android.gms:play-services-ads:15.0.0'
}
我错了哪里? 任何帮助将不胜感激 预先感谢
///////////////////////////////////////////////// //////////////////////////////////
答案 0 :(得分:1)
您的广告可以正常工作,我无需进行任何更改。使您的ImageButton
工作。我建议像这样将其移动到DrawerLayout
下面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="162dp"
android:layout_height="164dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
tools:context=".MainActivity"
android:layout_alignParentLeft="true">
<android.support.v7.widget.ContentFrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v7.widget.ContentFrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/holo_blue_bright">
</android.support.design.widget.NavigationView>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/ad"
android:layout_width="10dp"
android:layout_height="10dp"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</android.support.v4.widget.DrawerLayout>
<ImageButton
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="247dp"
android:layout_alignParentBottom="true"
android:background="@drawable/startearn" />
</RelativeLayout>
告诉我它是否适合您。
答案 1 :(得分:-2)
在您的app.gradle中,您是否已实施了这些工具?
在依赖项中:
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-ads:15.0.1'
依赖后:
apply plugin: 'com.google.gms.google-services'
我的admob活动:
// AdMob
AdView adView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
MobileAds.initialize(this, getString(R.string.admob_app_id));
//R.string.admob_app_id is a string resource with my app id]
如果继续进行而没有工作,则可以尝试使用横幅admob进行测试,然后再放入导航抽屉。