27
Android Studio 2.0 Preview 4

I am using to use BringToFront to get a TextView to display in front of the other controls.

The Doc's bringToFront() say you have to call requestlayout invalidate. Which I do, but doesn't work.

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

I am using this TextView inside a android.support.design.widget.CoordinatorLayout

However, the following code does work. But only supports API 21 and above. But I need to support API 16.

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

Or by setting the xml attribute property android:translationZ("4dp") works. However, only for API 21

4

3 回答 3

14
   /**
     * 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();

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

于 2015-12-29T07:38:52.247 回答
9

bringToFront()在里面为我工作android.support.design.widget.CoordinatorLayout。我的环境:

  • 安卓工作室 1.5.1
  • 设备:带有 Android 4.1.2 (API 16) 的摩托罗拉

活动主.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textStyle="bold"
        android:textSize="20sp"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>

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

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.bringToFront();
}

以下是截图:

textView.bringToFront();

在此处输入图像描述

没有textView.bringToFront();

在此处输入图像描述

于 2015-12-29T08:02:45.973 回答
8

在 KITKAT 之前,此方法后应调用视图父级的requestLayout() 和 invalidate()以强制父级使用新的子排序重绘。

这些方法必须在视图的父级上调用。您正在视图本身上调用它们。

这应该有效。

tvLevel.bringToFront();
tvLevel.getParent().requestLayout();
tvLevel.getParent().invalidate();
于 2015-12-29T07:21:49.233 回答