1

我的 Admob 的布局 XML 导致了一些错误。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-xxxxxxxxxxxxxxxxxxxxx"
    ads:loadAdOnCreate="true" />

<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/sailor" />

<TextView
    android:id="@+id/row"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/trans"
    android:padding="10dp"
    android:textColor="@color/menu_text"
    android:textSize="22sp"
    android:textStyle="normal" />

</LinearLayout>

活动

public class ListViewOne extends Activity {

// we use a string to hold the name of our extra,
// it must include the full package name
public final static String ID_EXTRA = "com.faarn.navyslang._ID";

private DatabaseHelper dbDataBaseHelper = null;
private Cursor ourCursor = null;
private DataBaseAdapter adapter = null;

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

        // this is our ListView element, obtained by id from our XML Layout
        ListView myListView = (ListView) findViewById(R.id.list_view);

        // create our database Helper
        dbDataBaseHelper = new DatabaseHelper(this);        
        // we call the create right after initializing the helper, just in
        // case
        // they have never run the app before
        dbDataBaseHelper.createDatabase();
        //
        // open the database!! Our helper now has a SQLiteDatabase database
        // object
        dbDataBaseHelper.openDataBase();
        // get our cursor. A cursor is a pointer to a dataset, in this case
        // a set of results from a database query
        ourCursor = dbDataBaseHelper.getCursor();
        // tell android to start managing the cursor
        // we do this just incase our activity is interrupted or ends, we
        // want the activity
        // to close and deactivate the cursor, as needed
        startManagingCursor(ourCursor);
        // create our adapter
        adapter = new DataBaseAdapter(ourCursor);
        // set the adapter!!!
        myListView.setAdapter(adapter);

        // this is how we know what to do when a list item is clicked
        myListView.setOnItemClickListener(onListClick);
    } catch (Exception e) {

        // this is the line of code that sends a real message to the Log
        Log.e("ERROR", "ERROR IN CODE: " + e.toString());

        // this is the line that prints out the location
        // the code where the error occurred.
        e.printStackTrace();
    }

}

private AdapterView.OnItemClickListener onListClick = new                    AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // Create our intent, as per usual
        Intent i = new Intent(ListViewOne.this, ListViewTwo.class);

        i.putExtra(ID_EXTRA, String.valueOf(id));
        TabGroupActivity parentActivity = (TabGroupActivity)getParent();
        parentActivity.startChildActivity("ListViewTwo", i);

    }
};

class DataBaseAdapter extends CursorAdapter {
    DataBaseAdapter(Cursor c) {
        super(ListViewOne.this, c);
    }

    @Override
    // this is a CursorAdapter
    // instead of Using a getView and if(row=null)
    // we use a bindView and newView calls
    // we can get away with this because CursorAdapters have
    // a default implementation of getView that calls bindView and newView
    // as needed. This makes our code a bit cleaner, and is the better way
    // to
    // do this
    public void bindView(View row, Context ctxt, Cursor c) {
        DataBaseHolder holder = (DataBaseHolder) row.getTag();
        holder.populateFrom(c, dbDataBaseHelper);
    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.main, parent, false);
        DataBaseHolder holder = new DataBaseHolder(row);
        row.setTag(holder);
        return (row);
    }
}

static class DataBaseHolder {
    private TextView name = null;

    DataBaseHolder(View row) {
        name = (TextView) row.findViewById(R.id.row);

    }

    void populateFrom(Cursor c, DatabaseHelper r) {
        name.setText(r.getName(c));
    }

}

}

Admob 正如预期的那样位于顶部,但也出现在我列表的每一行之间。我已经查看了很多关于此的其他问题,但无法纠正它。任何帮助表示赞赏。

4

2 回答 2

0

您描述的症状不是由您的布局 XML 中的任何内容引起的。要让 AdView 在您的列表中每隔 2 行出现,需要您的 ListAdapter 中的代码。你应该看看那里。

但是你的布局确实有问题

<TextView
    android:id="@+id/row"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

所写的此 TextView 将占用您的 LinearLayout 的全部高度

于 2013-11-13T21:48:46.633 回答
0

经过3天的反复试验,终于解决了这个问题。出于某种原因,将 TextView 和 ListView 放在同一个 XML 中会使 adMob 关闭。我删除了 TextView 并将其放入自己的 XML 布局中,代码现在可以工作了。然后,我需要在我的所有列表活动中将视图充气器从包含 admob 代码的 XML 更改为包含 TextView 代码的 XML。

新代码

主要 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-9438516449663554/1376593424"
    ads:loadAdOnCreate="true"
    ads:testDevices="TEST_EMULATOR, 37ae14af62d03e19" />

<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/sailor" />

</LinearLayout>

主行 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<TextView
    android:id="@+id/row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/trans"
    android:padding="10dp"
    android:textColor="@color/menu_text"
    android:textSize="22sp"
    android:textStyle="normal" />
</LinearLayout>

活动线

查看行 = inflater.inflate(R.layout.main, parent, false);

变成

查看行 = inflater.inflate(R.layout.main_row, parent, false);

于 2013-11-15T15:08:42.370 回答