【问题标题】:Visibility of the root of an inflated layout is changing with the child's visibility膨胀布局的根的可见性随着孩子的可见性而变化
【发布时间】:2020-02-14 23:14:15
【问题描述】:

我现在正在为一个学校项目做的 Android 应用程序发生了一个奇怪的错误/副作用。 最终目标应该是按下屏幕顶部的按钮后显示的视图。它最初是不可见的,然后切换。

但是,由于某种原因,它附加到的根视图也切换了其可见性,这恰好是它所在片段的主要布局。因此,最初的 setVisibility() 调用导致我的整个片段消失。

有什么原因吗?

编辑:为布局提供要附加的 FrameLayout 以解决问题。但是,问题仍然存在:是什么导致了这种行为?

代码:

        @Override
    public void onStart() {
        super.onStart();

        //prep timeselector view for transitions
        dashboardContainer = this.getView().findViewById(R.id.dashboardContainer);
        vgTimeSelector = getLayoutInflater().inflate(R.layout.viewgroup_timeselector,dashboardContainer);
        vgTimeSelector.setVisibility(View.INVISIBLE);

        //add timeselector popup
        bellButton = this.getView().findViewById(R.id.notificationButton);
        bellButton.setOnClickListener(togglerListener);
    }

private void toggleTimeSwitcherView () {

        //this is all animation stuff for timeselector
        int x = vgTimeSelector.getRight();
        int y = vgTimeSelector.getTop();
        int endRadius = (int) Math.hypot(vgTimeSelector.getWidth(),vgTimeSelector.getHeight());

        if (vgTimeSelector.getVisibility() == View.INVISIBLE) {
            vgTimeSelector.setVisibility(View.VISIBLE);
            Animator animator = ViewAnimationUtils.createCircularReveal(vgTimeSelector, x, y, 0, endRadius);
            animator.start();
        }
        else {
            Animator animator = ViewAnimationUtils.createCircularReveal(vgTimeSelector, x, y, endRadius, 0);
            animator.start();
            vgTimeSelector.setVisibility(View.INVISIBLE);
        }
    }

XML:


Fragment Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DashboardFragment"
    android:id="@+id/dashboardContainer">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/notificationButton"
        android:layout_alignParentRight="true"
        android:layout_margin="16dp"
        android:text="Bell"/> 

...

</RelativeLayout>

Child View:
<?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="wrap_content"
    android:padding="16dp"
    android:background="@color/darkGrey"
    android:id="@+id/timeselector"
    android:animateLayoutChanges="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="Notify me later"
        android:id="@+id/notifyTV"
        android:textSize="24sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_below="@id/notifyTV">

        <TimePicker
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:timePickerMode="spinner" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="N"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Y"/>

    </LinearLayout>

</RelativeLayout>

【问题讨论】:

    标签: android visibility layout-inflater


    【解决方案1】:

    您正在切换整个 dashboardContainer 的可见性。 我相信这个dashboardContainer.setVisibility(View.VISIBLE); 应替换为 vgTimeSelector.setVisibility(View.VISIBLE)

    dashboardContainer.setVisibility(View.INVISIBLE);vgTimeSelector.setVisibility(View.INVISIBLE)

    //this is all animation stuff for timeselector
            int x = vgTimeSelector.getRight();
            int y = vgTimeSelector.getTop();
            int endRadius = (int) Math.hypot(vgTimeSelector.getWidth(),vgTimeSelector.getHeight());
    
            if (vgTimeSelector.getVisibility() == View.INVISIBLE) {
                dashboardContainer.setVisibility(View.VISIBLE);
                Animator animator = ViewAnimationUtils.createCircularReveal(vgTimeSelector, x, y, 0, endRadius);
                animator.start();
            }
            else {
                Animator animator = ViewAnimationUtils.createCircularReveal(vgTimeSelector, x, y, endRadius, 0);
                animator.start();
                dashboardContainer.setVisibility(View.INVISIBLE);
            }
    

    【讨论】:

    • 嗯,这是一个问题,不是问题。即使改变了这个问题仍然存在。但是将其放入 FrameLayout 确实可以解决问题,但并不能解释这种行为
    猜你喜欢
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多