Dynamically add view to the bottom of RelativeLayout

坚强是说给别人听的谎言 提交于 2019-12-10 13:49:27

问题


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?

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.

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. solved I used this answer and it works perfect for me


回答1:


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

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

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);

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

rLayout.addView(yourAdView, rLParams);


来源:https://stackoverflow.com/questions/11018012/dynamically-add-view-to-the-bottom-of-relativelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!