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/