【问题标题】:fab.setVisibility(View.GONE) isn't workingfab.setVisibility(View.GONE) 不工作
【发布时间】:2017-01-18 15:32:53
【问题描述】:

我没有使用锚,它应该可以工作。 最初,可见性消失了(由 XML 设置)。当我按下一个按钮时,它变得可见(直到这里,工作)。然后,当我按下另一个按钮时,它应该消失了,但没有任何反应。 简化的 XML:

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/tbTela3"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ToolBar"
        app:titleTextColor="#757575"
        app:subtitleTextColor="#757575" />

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/cLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tbTela3">

        <ScrollView>
            [...]
        </ScrollView>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            android:elevation="8dp"
            android:src="@drawable/replay"
            android:visibility="gone"/>

    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

减少主要活动:

public void ZoomIn() {

    [...]

    zoomIn.setAnimationListener(new Animation.AnimationListener() {
        public void onAnimationStart(Animation anim) {}
        public void onAnimationRepeat(Animation anim) {}

        public void onAnimationEnd(Animation anim) {
            fab.setVisibility(View.VISIBLE); // WORKS FINE
        }
    });

    fab.startAnimation(zoomIn);
}

[...]

public void Clear() {

    [...]

    anim.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            readX.requestFocus();
            cardII.setVisibility(View.GONE); // WORKS FINE TOO
            fab.setVisibility(View.GONE); // BUT THIS DON'T

            fadeOut.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation anim) {}
                public void onAnimationRepeat(Animation anim) {}

                public void onAnimationEnd(Animation anim) {
                    clear.setVisibility(View.INVISIBLE); 
                }
            });

            clear.startAnimation(fadeOut);
        }
    });

    anim.start();

}

fab.setVisibility(View.GONE) 不能独立于 Clear 上的位置工作...我将代码简化为更具可读性,希望这不是问题。

【问题讨论】:

    标签: java android xml android-layout floating-action-button


    【解决方案1】:
            CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
            p.setAnchorId(View.NO_ID);
            fab.setLayoutParams(p);
            fab.setVisibility(View.GONE);
    

    或者试试

        fab.show(); //show
        fab.hide(); //hide
    

    【讨论】:

    • 我在其他问题中看到过这个,但我什至没有使用锚。没用。
    • .show 和 .hide 的作用完全相同:放大和缩小。谢谢!
    • 有什么方法可以编辑 .hide 动画属性吗? .show 需要和动画,但 .hide 应该会消失。
    • 由于某种原因,当我将 fab.show() 放在 ZoomIn 时,fab.setVisibility(View.GONE) 开始工作。现在我没有问题了。
    【解决方案2】:
    CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
            p.setAnchorId(View.NO_ID);
            fab.setLayoutParams(p);
            fab.setVisibility(View.invisible);
    

    不要使用 GONE

    隐形:

    这个视图是不可见的,但它仍然占用空间用于布局。

    走了:

    这个视图是不可见的,它不占用任何布局空间。

    隐形:

    调用适配器的getView()函数

    走了:

    适配器的 getView() 函数没有调用,从而阻止视图在不必要时加载

    【讨论】:

      【解决方案3】:

      现在一切正常。最终代码:

      public void ZoomIn() {
      
          [...]
      
          fab.show();
      }
      

      我删除了所有内容并放入了 fab.show()

      public void Clear() {
      
          [...]
      
          anim.addListener(new AnimatorListenerAdapter() {
              @Override
              public void onAnimationEnd(Animator animation) {
                  super.onAnimationEnd(animation);
                  readX.requestFocus();
                  cardII.setVisibility(View.GONE);
                  fab.setVisibility(View.GONE);
      
                  fadeOut.setAnimationListener(new Animation.AnimationListener() {
                      public void onAnimationStart(Animation anim) {}
                      public void onAnimationRepeat(Animation anim) {}
      
                      public void onAnimationEnd(Animation anim) {
                          clear.setVisibility(View.INVISIBLE); 
                      }
                  });
      
                  clear.startAnimation(fadeOut);
              }
          });
      
          anim.start();
      
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-31
        • 1970-01-01
        • 2015-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-31
        • 1970-01-01
        相关资源
        最近更新 更多