好的,我在XML&和这段代码,
我要做的是在编程中以(main.xml
TableLayout)设置linearLayout,同时仍然使用main.xml
这样内部活动的广告就会显示在线性布局的顶部。 main.xml中
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:baselineAligned="false"
android:orientation="vertical"
android:stretchColumns="1" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="@+id/start"
style="@android:style/Widget.Button.Inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start" />
<Button
android:id="@+id/help"
style="@android:style/Widget.Button.Inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/help" />
<Button
android:id="@+id/options"
style="@android:style/Widget.Button.Inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/options" />
</TableLayout>
和此文件Main.java
package com.ice_os.android.ad;
import java.util.Hashtable;
import com.ice_os.android.ad.R;
import com.inneractive.api.ads.InneractiveAd;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class Menu extends Activity {
LinearLayout linear;
Hashtable<InneractiveAd.IaOptionalParams, String> metaData = new Hashtable<InneractiveAd.IaOptionalParams, String>();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("InneractiveAd"));
linear.findViewById(R.id.linearLayout);
// Display banner ad
if (!InneractiveAd.displayAd(Menu.this,linear , "zeitue_PoundAPoliticianAdversion_Android", InneractiveAd.IaAdType.Banner, 120, Menu.this.metaData))
noConnectivity();
Button Start = (Button)findViewById(R.id.start);
Start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent StartIntent = new Intent(Menu.this,MainActivity.class);
startActivity(StartIntent);
//finish();
}
});
Button Help = (Button)findViewById(R.id.help);
Help.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent HelpIntent = new Intent(Menu.this,Help.class);
startActivity(HelpIntent);
}
});
Button Options = (Button)findViewById(R.id.options);
Options.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent OptionsIntent = new Intent(Menu.this,Options.class);
startActivity(OptionsIntent);
}
});
}
public Activity getActivity(){
return this.getActivity();
}
public void noConnectivity() {
AlertDialog alertDialog = new AlertDialog.Builder(Menu.this).create();
alertDialog.setTitle("This application is free but requires an internet connection");
alertDialog.setMessage("Please configure your connectivity settings and re-try");
alertDialog.setButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
onBackPressed();
} });
alertDialog.show();
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
}
};
@Override
protected void onDestroy() {
// Unregister events since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
}
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ice_os.android.ad"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" >
</activity>
<activity android:name=".Help" >
</activity>
<activity android:name="com.ice_os.android.ad.Options" >
</activity>
<activity android:name="com.inneractive.api.ads.InneractiveFullScreenView" >
</activity>
</application>
</manifest>
答案 0 :(得分:0)
好的,这就是我解决这个问题的方法。
main.xml不需要
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:baselineAligned="false"
android:orientation="vertical"
android:id="@+id/table"
android:stretchColumns="1" >
<Button
android:id="@+id/start"
style="@android:style/Widget.Button.Inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start" />
<Button
android:id="@+id/help"
style="@android:style/Widget.Button.Inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/help" />
<Button
android:id="@+id/options"
style="@android:style/Widget.Button.Inset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/options" />
</TableLayout>
Menu.java
package com.ice_os.android.ad.pound_a_politician;
import java.util.Hashtable;
import com.ice_os.android.ad.pound_a_politician.R;
import com.inneractive.api.ads.InneractiveAd;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
public class Menu extends Activity {
Hashtable<InneractiveAd.IaOptionalParams, String> metaData = new Hashtable<InneractiveAd.IaOptionalParams, String>();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TableLayout lnr = (TableLayout) findViewById(R.id.table);
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("InneractiveAd"));
if (!InneractiveAd.displayAd(Menu.this, lnr, "zeitue_PoundAPoliticianAdversion_Android", InneractiveAd.IaAdType.Banner, 120, Menu.this.metaData))
noConnectivity();
Button Start = (Button)findViewById(R.id.start);
Start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent StartIntent = new Intent(Menu.this,MainActivity.class);
startActivity(StartIntent);
}
});
Button Help = (Button)findViewById(R.id.help);
Help.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent HelpIntent = new Intent(Menu.this,Help.class);
startActivity(HelpIntent);
}
});
Button Options = (Button)findViewById(R.id.options);
Options.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent OptionsIntent = new Intent(Menu.this,Options.class);
startActivity(OptionsIntent);
}
});
}
public Activity getActivity(){
return this.getActivity();
}
public void noConnectivity() {
AlertDialog alertDialog = new AlertDialog.Builder(Menu.this).create();
alertDialog.setTitle("This application is free but requires an internet connection");
alertDialog.setMessage("Please configure your connectivity settings and re-try");
alertDialog.setButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
onBackPressed();
} });
alertDialog.show();
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@SuppressWarnings("unused")
String message = intent.getStringExtra("message");
}
};
@Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
}
基本上,您所做的是将要添加的布局部分分配给等效的布局数据类型
final TableLayout lnr = (TableLayout) findViewById(R.id.table);
然后您只需将内容添加到布局数据类型
if (!InneractiveAd.displayAd(Menu.this, lnr, "zeitue_PoundAPoliticianAdversion_Android", InneractiveAd.IaAdType.Banner, 120, Menu.this.metaData))
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ice_os.android.ad.pound_a_politician"
android:versionCode="4"
android:versionName="4.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:icon="@drawable/dark_icon"
android:label="@string/app_name" >
<activity
android:name=".Splash"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
<activity android:name=".Menu">
</activity>
<activity android:name=".Help" >
</activity>
<activity android:name=".Options" >
</activity>
<activity android:name="com.inneractive.api.ads.InneractiveFullScreenView" >
</activity>
<activity android:name=".GameOver"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
</application>
</manifest>
清单文件中的确保权限适用于广告服务
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />