简体   繁体   English

片段无意中落后于其他观点

[英]Fragment unintentionally behind other views

I have a container build like this: 我有这样的容器构建:

RelativeLayout container
--LinearLayout
  --TextView
--LinearLayout
  --Button
  --Button

And i am trying to add a Fragment "ListFragment" into the container showing ABOVE the two LinearLayouts with this: 我试图将一个片段“ListFragment”添加到容器中,显示两个LinearLayouts:

ListFragment listfragment = new ListFragment();
getFragmentManager().beginTransaction().replace(R.id.contianer,listfragment).commit();

The issue is: It doesnt. 问题是:它没有。 It only is displayed above the second LinearLayout, and the first LinearLayout is still visible ABOVE the Fragment. 它仅显示在第二个LinearLayout上方,并且第一个LinearLayout仍然可见于片段上方。

Here are both xmls: 这是两个xmls:

Container xml: 容器xml:

<RelativeLayout
        android:id="@+id/contianer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/container">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:layout_marginLeft="0dp"
            android:id="@+id/linearLayout"
            android:layout_alignParentBottom="true"
            android:paddingTop="30dp">

            <Button
                android:id="@+id/back"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button"
                android:textSize="30dp" />

            <Button
                android:id="@+id/nextButton"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_weight="0.5"
                android:text="Button"
                android:textSize="30dp"
                android:background="#009688"
                android:textColor="#ffffff" />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:elevation="4dp"
            android:id="@+id/vanisherView"
            android:layout_centerHorizontal="true"
            android:layout_margin="20dp">

            <TextView
                android:id="@+id/erklareung"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:background="@android:color/white"
                android:padding="40dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="35dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true" />

        </LinearLayout>


    </RelativeLayout>

The container is wrapped in another Layout, but i think, that doesnt really matter. 容器包装在另一个布局中,但我认为,这并不重要。

Here is the Fragment xml: 这是片段xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:id ="@+id/viewer_layout">

    <com.woxthebox.draglistview.DragListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drag_list_view"
        android:layout_width="match_parent"
        android:background="@android:color/black"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_height="match_parent"/>

</RelativeLayout>

Ive painted the Fragment Black, to see where it is shown, and where not 我画了片段黑色,看它在哪里显示,哪里没有

Thanks for answers and cheers 谢谢你的回答和欢呼

Don't try to add a Fragment to a container view that already contains other views. 不要尝试将Fragment添加到已包含其他视图的容器视图中。

Instead, place a FrameLayout wherever you want the Fragment to be placed and then add your Fragment to that instead of the parent container. 而是将FrameLayout放置在您希望放置Fragment的任何位置,然后将Fragment添加到其中而不是父容器。

You can set the required attributes on the FrameLayout to ensure it is placed where you want it within your RelativeLayout . 您可以在FrameLayout上设置所需的属性,以确保它位于RelativeLayout

Example: 例:

<RelativeLayout android:id="@+id/container>"

    <FrameLayout android:id="@+id/fragment_container"/>

    <LinearLayout>
        <Button/>
        <Button/>
    </LinearLayout>

    <LinearLayout>
        <TextView/>
    </LinearLayout>

</RelativeLayout>

You can wrap your layout into FrameLayout 您可以将布局包装到FrameLayout

FrameLayout
--RelativeLayout   //layout with your views
--FrameLayout      //container for your fragment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM