【问题标题】:How to inflate a view behind another如何在另一个视图后面膨胀
【发布时间】:2016-02-06 20:49:53
【问题描述】:

如果这是一个简单的问题,请原谅我,但是我是 Android 开发的新手,这个问题一直困扰着我。

我有一个“基础”布局文件,其中包含来自this library 的 RelativeLayout 和 com.devspark.sidenavigation.SideNavigationView。

然后我有一个带有 ViewPager 的活动,它将其他布局(包含 ListViews 和progressBars)膨胀到这个中。我的问题是,无论我如何尝试,SideNavigationMenu 总是出现在膨胀的列表视图后面。我不确定出了什么问题,而且我迷失在一堆乱七八糟的布局中。有没有人指点一下?

这是我的“基本”布局:

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

以及膨胀的“子”布局之一的示例:

<?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>

提前致谢

【问题讨论】:

    标签: android android-layout android-xml


    【解决方案1】:

    相对布局不支持图层。

    视图按照添加到布局中的顺序放置在彼此之上。

    例如

    • 将 viewA 添加到相对布局
    • 将 viewB 添加到相对布局
    • viewB 将在 viewA 前面

    在“后面”膨胀的一种方法是在另一个视图膨胀之前膨胀另一个视图。

    • 将 viewB 添加到相对布局
    • 将 viewA 添加到相对布局
    • viewA 将在 viewB 的前面

    或删除视图并将其重新添加到相对布局

    • 将 viewA 添加到相对布局
    • 将 viewB 添加到相对布局
    • 移除视图A
    • 重新添加viewA
    • viewA 将在 viewB 的前面

    【讨论】:

    • 做到了。当我在 onCreateView() 中添加内容时,我感到很困惑。这个返回语句为布局添加了一个视图,因此返回侧边菜单并在工作之前进行添加/删除。非常感谢!
    【解决方案2】:

    我是这样做的:

    parentView.addView(childView, 0);
    

    第二个参数是index,添加child的位置

    这里的第二个参数在 0 处传递,这使得子视图被添加到已经存在的视图的后面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多