动态将视图添加到RelativeLayout的底部 [英] Dynamically add view to the bottom of RelativeLayout

查看:622
本文介绍了动态将视图添加到RelativeLayout的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在main.xml中有TitlePageIndicator和ViewPager,并且我想在RelativeLayout的底部添加admob(直接到LinearLayout中)。我该怎么办?

I have in main.xml TitlePageIndicator and ViewPager, and I want to add admob (right into LinearLayout) at the bottom of RelativeLayout. How can I do this?

启动它时,日志中没有任何关于错误的信息(因为没有放置admob的地方),但是admob是不可见的,我可以没看到。 (看起来admob在屏幕外,因为我试图将ViewPager设置为特定大小,并且效果很好)
我不想为ViewPager设置特定大小(由于屏幕大小不同)
谢谢。

When I launch it, the log says nothing about errors (since there is no place to put admob), but admob is invisible, I can`t see it. (looks like admob is outside the screen because I tried to set specific sizes to ViewPager and it works perfect) I do not want to set specific sizes to ViewPager (becouse of the different screen sizes) Thanks.

我的main.xml是:

My main.xml is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.viewpagerindicator.TitlePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"/>   
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/indicator"/>
    <LinearLayout 
        android:id="@+id/for_ads"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/viewpager"/>
</RelativeLayout>

UPD。
解决了
我用了这个答案,它对我来说很完美

UPD. solved I used this answer and it works perfect for me

推荐答案

首先,如果您要引用RelativeLayout,则需要一个ID:

First, your RelativeLayout needs an ID if you want to reference it:

RelativeLayout rLayout = (RelativeLayout)findViewById(R.id.yourRelativeId);

然后为对象(在本例中为admob adview)创建一些LayoutParams来告诉它对齐本身位于底部(并且不与其他任何视图对齐,因此不会被其他屏幕推开或移动):

Then create some LayoutParams for the object (in this case, your admob adview) that tell it to align itself to the bottom (and not align to any other views, this way it isn't pushed off-screen or moved by the other views):

 RelativeLayout.LayoutParams rLParams = 
    new RelativeLayout.LayoutParams(
    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    rLParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);

接下来,使用LayoutParams将视图添加到RelativeLayout中:

Next, add the view to your RelativeLayout with your LayoutParams:

rLayout.addView(yourAdView, rLParams);

这篇关于动态将视图添加到RelativeLayout的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆