【问题标题】:Added fragment shows under elevated views in fragment below?在下方片段的提升视图下添加片段显示?
【发布时间】:2018-07-25 20:59:42
【问题描述】:

所以我在将我的对话片段添加到我的活动时遇到了问题。该片段是使用

添加的常规片段
supportFragmentManager.beginTransaction().add(...).commit()

问题是片段被添加到活动中,但活动的工具栏和浮动操作按钮将显示在片段顶部。我知道这是因为这两个视图被提升到 4dp 并且可以通过将片段高度更改为

活动

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:elevation="4dp"
    android:gravity="center"
    android:text="@string/app_name"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
    app:layout_constraintTop_toTopOf="parent" />

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar"/>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_margin="24dp"/>

对话框片段

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:background="#40000000">

<FrameLayout
    android:elevation="4dp"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="250dp"/>

【问题讨论】:

    标签: android android-fragments android-elevation


    【解决方案1】:

    提供的答案与其说是解决方案,不如说是一种技巧。当用户在片段之间来回导航时,隐藏、显示、重新隐藏和重新显示是一种痛苦。更好的解决方案是拥有一个 RelativeLayout(如果同时使用多个片段)或 LinearLayout/FrameLayout 父级,其中包含您的 ConstraintLayout。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
    <TextView
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:elevation="4dp"
        android:gravity="center"
        android:text="@string/app_name"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
        app:layout_constraintTop_toTopOf="parent" />
    
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"/>
    
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_margin="24dp"/>
    
        </android.support.constraint.ConstraintLayout>
    
    </RelativeLayout>
    

    不要忘记包含您的 id,例如rl_container 用作片段容器的引用。请记住,ConstraintLayout 是一个平面层次结构。因此,将其用作容器总是会遇到任何“提升”(android:elevation="xdp")或对话框或其他任何不平坦的视图的问题。干杯!

    【讨论】:

      【解决方案2】:

      活动开始时,尝试使用可见性隐藏视图,这是最简单的方法,floatingActionButton.setVisibility(View.GONE);

      【讨论】:

        【解决方案3】:

        为了隐藏floatingActionButton,在你的fragmentonCreateView中添加以下内容:

        //Global Variable
        private View view;
        
        @Override
        public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
        if (view != null) {
                ViewGroup group = (ViewGroup) view.getParent();
                if (group != null) {
                    group.removeView(view);
                }
            } else {
        floatingActionButton = view.findViewById(R.id. floatingActionButton);
        floatingActionButton.hide();
        
        ...
        
        return view;
        }
        

        在您的 xml 中:

        <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        //If this is your toolbar, why are you instantiating a textview and not a toolbar???
        <TextView
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:elevation="4dp"
            android:gravity="center"
            android:text="@string/app_name"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
            app:layout_constraintTop_toTopOf="parent" />
        
        // get rid off below toolbar so that the container will cover the whole height and width of the screen
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"/>
        
        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_margin="24dp"/>
        

        【讨论】:

        • 虽然我确信你的回答很好,但它并没有回答我的问题。我的问题是微不足道的,因为我想更好地了解事情是如何运作的。我猜片段管理器只是将片段的视图添加到 ViewGroup 的底部,这就是为什么它位于具有相同高度的任何视图之上,但低于该 ViewGroup 中高度高于片段高度的视图。也许我的问题是一个不好的问题,但我想知道是否已经存在一种解决方案,其中添加片段会使 ViewGroup 在高程方面变平,因此片段位于顶部或其他什么...
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-09
        相关资源
        最近更新 更多