在片段中的列表视图下方显示admob横幅广告

时间:2017-03-17 17:53:01

标签: android listview admob android-linearlayout android-relativelayout

我正在尝试在片段A的列表视图下方显示Admob横幅广告。出于某种原因,它不会像片段B中那样显示,我将广告放在一组文字视图下方。

在片段B中,广告显示在屏幕底部的中心位置,并且显示在"上方" 可拖动的textViews集中。

在片段A中,广告显示在屏幕底部的明确区域中,与listView位于同一"层" 中。 如果我没有指定50 dp marginBottom属性,则列表视图不会显示。

这是片段A的布局xml:

@IBAction func addCommentButtonPressed(_ sender: Any) {

    if !addCommentTextField.text!.isEmpty {
        addComment(comment: addCommentTextField.text!)
        observePostComments()
    }
}

func addComment(comment: String) {
    let postsCommentsRef = FIRDatabase.database().reference().child("postComments").child(self.postID)
    var commentData:  [String: String] = [:]
    commentData["userId"] = FIRAuth.auth()!.currentUser!.displayName!
    commentData["comment"] = comment
    postsCommentsRef.childByAutoId().setValue(commentData)
}


func observePostComments() {
    let postsCommentsRef = FIRDatabase.database().reference().child("postComments").child(self.postID)
    postsCommentsRef.observe(.childAdded, with: { snapshot in
        let comment = snapshot.value as! [String: String]
        self.selectedPost.commentAuthors.append(comment["userId"]!)
        self.selectedPost.commentText.append(comment["comment"]!)
        self.tableView.reloadData()
    })
}

这是片段B的布局xml:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CommentCell

    cell.commentAuthorLabel?.text = selectedPost.commentAuthors[indexPath.row]
    cell.commentLabel?.text = selectedPost.commentText[indexPath.row]

    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return selectedPost.commentAuthors.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 72
}

如何实现与片段A中片段B中看到的效果相同的效果?我不希望片段A中的当前显示,因为如果广告由于某种原因没有加载,屏幕底部的空白区域将显得难看。如果至少在此处看到片段B的效果,那么即使广告未加载,屏幕看起来也完好无损。

此外,一旦完成,可以采取哪些措施来避免广告与列表中的最后一个元素重叠?

1 个答案:

答案 0 :(得分:1)

尝试将ListView置于AdView

之上
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/windowBackground">

     <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"/>

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/adView"/>

</RelativeLayout>
相关问题