广告覆盖界面而不是出现在它上面

时间:2013-04-10 17:39:16

标签: android xml user-interface overlay admob

首先,我要感谢大家在此前的问题上给予的帮助。你们都是一个无法估量的帮助。这有望成为我关于这个特定程序的最后一个问题,如果它已经被回答,请将我链接到我通过搜索找不到的地方。现在发生的事情是,不是横幅广告出现并将界面的其余部分向下移动,它显示在我的UI顶部,并阻止访问其下方的UI部分。除此之外,一切都很好。这是XML和JAVA。

Layout.XML

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

<!-- Ad Placeholder -->

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

<ScrollView
    android:id="@+id/BaseScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

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

        <LinearLayout
            android:id="@+id/LinearLayoutH1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="left"
                android:text="@string/Sad" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="@string/Happy" />
        </LinearLayout>

        <SeekBar
            android:id="@+id/happinessBarID"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:max="@integer/SliderMax"
            android:progress="@integer/SliderDefault" />

        <LinearLayout
            android:id="@+id/LinearLayoutH2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="left"
                android:text="@string/Tired" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="@string/Awake" />
        </LinearLayout>

        <SeekBar
            android:id="@+id/energyBarID"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:max="@integer/SliderMax"
            android:progress="@integer/SliderDefault" />

        <LinearLayout
            android:id="@+id/LinearLayoutH3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="left"
                android:text="@string/Calm" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="@string/Anxious" />
        </LinearLayout>

        <SeekBar
            android:id="@+id/anxietyBarID"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:max="@integer/SliderMax"
            android:progress="@integer/SliderDefault" />

        <LinearLayout
            android:id="@+id/LinearLayoutH4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView7"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="left"
                android:text="@string/No_Pain" />

            <TextView
                android:id="@+id/textView8"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="@string/Max_Pain" />
        </LinearLayout>

        <SeekBar
            android:id="@+id/painBarID"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:max="@integer/SliderMax"
            android:progress="@integer/SliderDefault" />

        <DatePicker
            android:id="@+id/datePicker1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TimePicker
            android:id="@+id/timePicker1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/noteTextFieldID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/Note_Hint"
            android:inputType="textMultiLine"
            android:maxLength="@integer/NoteLimit" />

        <Button
            android:id="@+id/enterButtonID"
            android:layout_width="132dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:onClick="dialogPop"
            android:text="@string/EnterButtonText" />
    </LinearLayout>
</ScrollView>

</RelativeLayout>

和Java文件......

package com.loch.meaptracker;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.google.ads.*;


import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TimePicker;

public class MainActivity extends Activity implements OnSeekBarChangeListener {

private SeekBar happyBar, energyBar, anxietyBar, painBar;
private EditText noteField;
private DatePicker dPick;
private TimePicker tPick;
@SuppressWarnings("unused")
private Button enterButton;
private int happyValue = 4, energyValue = 4, anxietyValue = 4,
        painValue = 4;
private static final String TAG = "heapApp";
private String Mood = "Blah";
private AdView adView;
final Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adView
        adView = new AdView (this, AdSize.BANNER, "a15138b1a7adad2");

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

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

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

        // bars
        happyBar = (SeekBar) findViewById(R.id.happinessBarID);
        happyBar.setOnSeekBarChangeListener(this);
        energyBar = (SeekBar) findViewById(R.id.energyBarID);
        energyBar.setOnSeekBarChangeListener(this);
        anxietyBar = (SeekBar) findViewById(R.id.anxietyBarID);
        anxietyBar.setOnSeekBarChangeListener(this);
        painBar = (SeekBar) findViewById(R.id.painBarID);
        painBar.setOnSeekBarChangeListener(this);
        // end bars
        dPick = (DatePicker) findViewById(R.id.datePicker1);
        tPick = (TimePicker) findViewById(R.id.timePicker1);
        noteField = (EditText) findViewById(R.id.noteTextFieldID);
        enterButton = (Button) findViewById(R.id.enterButtonID);
    } catch (Exception onCreateException) {
        Log.e(TAG, "Exception received", onCreateException);
    }
}

@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_main, menu);
    return true;
}

// Bar listener methods
@Override
public void onProgressChanged(SeekBar arg0, int barValue, boolean hFromUser) {
    try {

        switch (arg0.getId()) {
        case R.id.happinessBarID:
            happyValue = barValue + 1;
            break;
        case R.id.energyBarID:
            energyValue = barValue + 1;
            break;
        case R.id.anxietyBarID:
            anxietyValue = barValue + 1;
            break;
        case R.id.painBarID:
            painValue = barValue + 1;
            break;
        }
        String debugBarValue = "Happy is " + happyValue + ", Energy is "
                + energyValue + ", Anxiety is " + anxietyValue
                + ", Pain is " + painValue + ".";
        System.out.println(debugBarValue);

    } catch (Exception BarValueException) {
        Log.e(TAG, "Exception received", BarValueException);
    }

}

@Override
public void onStartTrackingTouch(SeekBar happyBar) {
    // TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar happyBar) {
    // TODO Auto-generated method stub

}

// end Bar listener methods

// Enter Button listener Method

public void dialogPop(View v) {
    try {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

        // set Title
        alertDialogBuilder.setTitle("title");

        // set dialog message
        alertDialogBuilder.setMessage("You entered: " + getMood())
                .setCancelable(false).setPositiveButton("Okay",
                // When Okay button clicked the write mood string to file
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                try {
                                    // This is the string that should be
                                    // written to file
                                    String data = getMood();
                                    // This is the file that should be
                                    // written to
                                    File heapFile = new File(Environment.getExternalStorageDirectory(), "heapFile.csv");

                                    // if file doesn't exists, then create
                                    // it
                                    if (!heapFile.exists()) {
                                        heapFile.createNewFile();
                                    }

                                    // true = append file
                                    FileWriter heapFileWritter = new FileWriter(
                                            heapFile.getName(), true);
                                    BufferedWriter heapBufferWritter = new BufferedWriter(
                                            heapFileWritter);
                                    heapBufferWritter.write(data);
                                    heapBufferWritter.close();

                                    System.out.println("Done");

                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

                        })
                // If they press either the cancel button or the back button
                // on their device (Same thing) then close the dialog and
                // give the user a chance to change what they've entered
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // TODO Auto-generated method stub
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    } catch (Exception buttonListenerException) {
        Log.e(TAG, "Exception received", buttonListenerException);
    }
    return;
}

public String getMood() {
    try {
        int month = dPick.getMonth();
        int day = dPick.getDayOfMonth();
        int year = dPick.getYear();
        int minute = tPick.getCurrentMinute();
        String moodAntePost = "AM";
        boolean hourType = tPick.is24HourView();
        int moodHour = tPick.getCurrentHour();
        if (hourType == false && moodHour > 12) {
            moodHour = (moodHour - 12);
            moodAntePost = "PM";
        } else if (hourType == false && moodHour <= 0) {
            moodHour = 12;
        } else {
        }
        String noteText = noteField.getText().toString();
        Mood = "Happiness," + happyValue + ",Energy," + energyValue
                + ",Anxiety," + anxietyValue + ",Pain," + painValue
                + ",Date," + month + "/" + day + "/" + year + ",Time,"
                + moodHour + ":" + minute + "," + moodAntePost + ",Note,"
                + noteText;
        System.out.println(Mood);
    } catch (Exception getMoodException) {
        Log.e(TAG, "Exception received", getMoodException);
    }

    return Mood;
}

}

1 个答案:

答案 0 :(得分:1)

您可以选择使用LinearLayout替换外部RelativeLayout,也可以在Scrollview中放置android:layout_below="@id/adView"

顺便说一句,你的内部布局看起来很嵌套。您应该考虑将其替换为relativelayout或gridlayout以获得更好的性能。