android - BringToFront 在协调器布局中不起作用

标签 android bringtofront

Android Studio 2.0 Preview 4

我使用 BringToFront 来获取 TextView 以显示在其他控件的前面。

文档的bringToFront()假设你必须调用 requeSTLayout invalidate。我这样做了,但不起作用。

tvLevel.bringToFront();
tvLevel.requestLayout();
tvLevel.invalidate();

我在 android.support.design.widget.CoordinatorLayout

中使用这个 TextView

但是,以下代码确实有效。但只支持 API 21 及以上。但我需要支持 API 16。

  if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      tvLevel.setTranslationZ(4);
      tvLevel.invalidate();
  }

或者通过设置 xml 属性属性 android:translationZ("4dp") 起作用。但是,仅适用于 API 21

最佳答案

   /**
     * Change the view's z order in the tree, so it's on top of other sibling
     * views. This ordering change may affect layout, if the parent container
     * uses an order-dependent layout scheme (e.g., LinearLayout). Prior
     * to {@link android.os.Build.VERSION_CODES#KITKAT} this
     * method should be followed by calls to {@link #requestLayout()} and
     * {@link View#invalidate()} on the view's parent to force the parent to redraw
     * with the new child ordering.
     *
     * @see ViewGroup#bringChildToFront(View)
     */
    public void bringToFront() {
        if (mParent != null) {
            mParent.bringChildToFront(this);
        }
    }

根据这个你可能会漏掉这一行:

((View)myView.getParent()).requestLayout();

它会起作用的,检查一下。!

关于android - BringToFront 在协调器布局中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469596/

相关文章:

android - 如何在 SceneForm 中使用 Camera2 API 录制视频

ios - 将 View 置于最前面,即使它的层次结构顺序不正确

html - Smooch 聊天上面有元素,我该如何解决这个问题?

delphi - 如何将被另一个窗口隐藏的 OpenDialog 带到前面

android - 从 Android 客户端将参数传递到 Windows Azure 上托管的 VB.NET WS

java - 共享内存(int)与java线程的同步

android - 在 Android 的 AlertDialog 中更改 CheckBox 样式

android - 如何发布具有另一个 android 库项目依赖项的 AAR?