How to inflate a view behind another

戏子无情 提交于 2020-01-05 08:43:13

问题


Forgive me if this is a simple question however I am new to Android Development and this problem has been bugging me.

I have a "base" layout file which contains a RelativeLayout and a com.devspark.sidenavigation.SideNavigationView from this library.

I then have an activity with a ViewPager which inflates other layouts (containing ListViews and progressBars) into this. My issue is that however I try and do it, the SideNavigationMenu always appears behind the inflated listviews. I'm not sure what's going wrong and I'm getting lost in a mess of Layouts. Does anyone have any pointers?

Here is my "base" layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NewNews"
    tools:ignore="MergeRootFrame">  

<com.devspark.sidenavigation.SideNavigationView
    android:id="@+id/side_navigation_view_news"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"/>

</RelativeLayout>

and an example of one of the "sub"-layouts which are inflated:

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

<eu.erikw.PullToRefreshListView
    android:id="@+id/listView_all"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"/>

<ProgressBar
    android:id="@+id/progressBar_all"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

</FrameLayout>

Thanks in advance


回答1:


Relative layout doesn't support layers.

Views are placed on top of each other by the order they are added to the layout.

e.g.

  • add viewA to relativelayout
  • add viewB to relativelayout
  • viewB will be infront of viewA

One way to inflate "behind" is inflating the other view before the other one is inflated.

  • add viewB to relativelayout
  • add viewA to relativelayout
  • viewA will be infront of viewB

or remove the view and re-add it to the relativelayout

  • add viewA to relativelayout
  • add viewB to relativelayout
  • remove viewA
  • re-add viewA
  • viewA will be infront of viewB



回答2:


I did it like this:

parentView.addView(childView, 0);

Second parameter is index, the position at which to add the child

here second parameter is passed at 0 which makes the child view to be added in the back of views already present.



来源:https://stackoverflow.com/questions/14835735/how-to-inflate-a-view-behind-another

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