簡體   English   中英

如何在片段之上保持視圖

[英]How to keep a View on top of my Fragments

聽起來像是一個以前已經回答過的問題,但是從我的研究中我找不到解決方案。 我的布局大致如下:

在此處輸入圖片說明

主要區域是我將在其中添加/替換片段的容器,底部是我放置底部導航菜單的容器。 粉紅色的“視圖”是我需要放在菜單頂部的按鈕(該按鈕不起作用)。

每次將Fragment添加到主視圖時,即使將TextView添加到容器View之后,“菜單”文本也會消失。 與粉紅色的Button相同,該按鈕添加在底部菜單容器之后(請參見下面的xml)。

如何將“菜單” TextView和粉紅色的Button始終保留在片段頂部?

這是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eee"
    tools:context="com.wecancer.wecancer.activities.Menu">

    <FrameLayout
        android:id="@+id/main_container_view"
        android:layout_width="match_parent"
        android:layout_height="520dp"
        android:layout_marginBottom="0dp"
        >
    </FrameLayout>

    <TextView
        android:id="@+id/main_center_tv"
        android:layout_centerVertical="true"
        android:textSize="24sp"
        android:layout_centerHorizontal="true"
        android:text="@string/menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:layout_alignParentBottom="true"
        android:id="@+id/main_bottom_menu_container"
        android:layout_width="match_parent"
        android:background="@color/lightGray"
        android:layout_height="40dp"
        >

    </FrameLayout>

    <Button
        android:elevation="1dp"
        android:id="@+id/menu_plus_img"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:background="@color/colorAccent"
        android:layout_width="70dp"
        android:layout_marginBottom="0dp"
        android:layout_height="70dp"
        tools:targetApi="lollipop" />

</RelativeLayout>

試試這個`

<FrameLayout
    android:layout_alignParentBottom="true"
    android:id="@+id/main_bottom_menu_container"
    android:layout_width="match_parent"
    android:background="@color/colorPrimary"
    android:layout_height="40dp"
    >

</FrameLayout>

<Button
    android:elevation="1dp"
    android:id="@+id/menu_plus_img"
    android:background="@color/colorAccent"
    android:layout_width="70dp"
    android:layout_height="70dp"
    tools:targetApi="lollipop"
    android:layout_marginLeft="71dp"
    android:layout_marginStart="71dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:id="@+id/main_center_tv"
    android:textSize="24sp"
    android:text="@string/menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_alignBaseline="@+id/menu_plus_img"
    android:layout_alignBottom="@+id/menu_plus_img"
    android:layout_toRightOf="@+id/menu_plus_img"
    android:layout_toEndOf="@+id/menu_plus_img" />

<FrameLayout
    android:id="@+id/main_container_view"
    android:layout_width="match_parent"
    android:layout_height="520dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/menu_plus_img">


</FrameLayout>

`

您只需要在使用Framelayout時更改布局中視圖的順序即可

布局的復雜性不需要RelativeLayout,因此您只需要切換即可。

我真的不知道為什么諸如putToFrontBringChildToFront或添加和刪除View的子代以更改其索引的方法不起作用。 因此,我實際上必須創建一個新的布局結構。

代替:

RelativeLayout
...FrameLayout
...TextView
...FrameLayout
...Button

我做了:

FrameLayout
...RelativeLayout
......FrameLayout
......FrameLayout
......TextView
...RelativeLayout
......Button

而第二個RelativeLayout僅具有一個按鈕和透明背景。 我的完整xml代碼在下面作為參考。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_root_view"
    android:layout_height="match_parent">

    <RelativeLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#eee"
        tools:context="com.wecancer.wecancer.activities.Menu">

        <FrameLayout
            android:layout_alignParentBottom="true"
            android:id="@+id/main_bottom_menu_container"
            android:layout_width="match_parent"
            android:background="@color/lightGray"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@+id/main_container_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="0dp" />

        <TextView
            android:id="@+id/main_center_tv"
            android:layout_centerVertical="true"
            android:textSize="24sp"
            android:layout_centerHorizontal="true"
            android:text="@string/menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:onClick="menuPlusBt"
            android:elevation="5dp"
            android:id="@+id/menu_plus_img"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:background="@drawable/green_circle"
            android:layout_width="70dp"
            android:layout_marginBottom="0dp"
            android:layout_height="70dp"
            tools:targetApi="lollipop" />

    </RelativeLayout>

</FrameLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM