将视图动态添加到RelativeLayout的底部

Zak*_*man 6 android relativelayout admob

我在main.xml TitlePageIndicator和ViewPager中,我想在RelativeLayout的底部添加admob(右侧为LinearLayout).我怎样才能做到这一点?

当我启动它时,日志没有说明错误(因为没有地方放置admob),但是admob是看不见的,我看不到它.(看起来像admob在屏幕之外,因为我试图为ViewPager设置特定尺寸并且它工作完美)我不想为ViewPager设置特定尺寸(因为不同的屏幕尺寸)谢谢.

我的main.xml是:

<?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>
Run Code Online (Sandbox Code Playgroud)

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

Cru*_*ceo 19

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

RelativeLayout rLayout = (RelativeLayout)findViewById(R.id.yourRelativeId);
Run Code Online (Sandbox Code Playgroud)

然后为对象创建一些LayoutParams(在这种情况下,你的admob adview)告诉它将自己对齐到底部(并且不与任何其他视图对齐,这样它不会被推离屏幕或被其他视图移动):

 RelativeLayout.LayoutParams rLParams = 
    new RelativeLayout.LayoutParams(
    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    rLParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
Run Code Online (Sandbox Code Playgroud)

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

rLayout.addView(yourAdView, rLParams);
Run Code Online (Sandbox Code Playgroud)