67

我无法将视图发送回去。在 Android 中,我们有一个类似bringToFront(), 的方法来将视图放在另一个视图的顶部。像这样,我想把视图放在上一张图片的下方。

有没有类似sendToBack()bringToBack()在Android中的任何方法。如果是这样,任何人都可以帮助我。

注意:我不想通过在布局中放置项目的顺序来控制 z 顺序,我想以编程方式控制 z 顺序。

我不想隐藏前面的视图,我只想让它们位于正在移动的视图后面。

4

13 回答 13

67

我意识到这已在其他答案中暗示,但没有人发布代码。我将以下内容保存在一个实用程序类中,其中我有处理视图的“帮助”函数:

public static void sendViewToBack(final View child) {
    final ViewGroup parent = (ViewGroup)child.getParent();
    if (null != parent) {
        parent.removeView(child);
        parent.addView(child, 0);
    }
}
于 2013-11-09T05:43:42.847 回答
42

没有sendToBack()方法。但是,如果您调用bringToFront()较低位置的视图,您将获得几乎相同的效果。

于 2012-02-16T13:37:14.857 回答
36

Afaik 没有内置的解决方案。我猜您正在尝试修改 FrameLayout 或类似内容中的 z 顺序。

但是,我认为您可以修改布局中包含的子元素的顺序。RemoveChild...() 和 addView() 方法可以获取位置值,因此您很可能会交换子元素,这会修改 z 顺序。然而,这似乎有点 hacky 解决方案。

或者考虑修改子视图的可见性属性,你可能会得到类似的行为,我认为这会更干净。

编辑:

有了新的 Android 版本,L 开发者预览版,似乎我们终于能够轻松地更改视图的 Z 顺序。'Elevation' 和 'TranslationZ' 属性来救援:https ://developer.android.com/preview/material/views-shadows.html

于 2011-07-20T09:19:42.100 回答
19

在前面调用bringToFront()你想要的视图,然后invalidate()在所有视图上调用方法,包括你想要在前面的视图。对所有听众重复相同的操作。

因此,当另一个视图的侦听器被调用时,前一个视图将失效并处于后台。

于 2012-02-21T13:14:31.597 回答
9

这是我用来将视图发送到后面的方法(与bringToFront相反,一种sendToBack):

    private void moveToBack(View myCurrentView) 
    {
        ViewGroup myViewGroup = ((ViewGroup) myCurrentView.getParent());
        int index = myViewGroup.indexOfChild(myCurrentView);
        for(int i = 0; i<index; i++)
        {
            myViewGroup.bringChildToFront(myViewGroup.getChildAt(i));
        }
    }

希望这可以帮助!

于 2013-06-29T00:45:02.683 回答
2

请注意,您可以view.setZ(float)从 API 级别 21 开始使用。在这里您可以找到更多信息。

于 2015-10-23T15:08:37.447 回答
2

它工作得很好,就像 bringToBack()

public void moveToBack(ViewGroup viewGroup, View v) {
    int s = 1;
    for (int i = 1; i < viewGroup.getChildCount(); i++) {
        if (viewGroup.getChildAt(1) == v) {
            s = 2;
        } else {
            viewGroup.getChildAt(s).bringToFront();
        }
    }
}
于 2017-09-21T16:30:10.360 回答
2

如果您使用的是 appCompat 库,还有另一种方法可以更改低于 21 API 级别的设备的 Z 顺序:

ViewCompat.setTranslationZ(view, zValue);
于 2018-03-19T06:15:08.457 回答
2

在您的 XML 文件中,您必须按顺序编写:

<FrameLayout
android:id="@+id/frameLayoutXML"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
<ImageView
 android:id="@+id/imageViewXML"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatB_XML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>

在这种情况下,imageView将在FloatingActionButton.

如果您想更改某些内容,并且将以编程方式进行,那么:

public class SomeActivity extends AppCompatActivity {

    //declare variables
    ImageView imageView;
    FrameLayout frameLayout;
    FloatingActionButton floatingActionButtonNew;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //Here is to call what already exists at XML file
        frameLayout=findViewById(R.id.frameLayoutXML);   
        imageView = findViewById(R.id.imageViewXML);
        //Here is to declare a new Element to add to the view, but is not at XML File
        floatingActionButtonNew=New FloatingActionButton(this);
        frameLayout.removeAllViews();  //here you remove all views.
        frameLayout.addView(imageView);//And you add again the imageView first
        floatingActionButtonNew.setLayoutParams(new 
         RelativeLayout.LayoutParams(
          RelativeLayout.LayoutParams.WRAP_CONTENT,
          RelativeLayout.LayoutParams.WRAP_CONTENT
        )); //Then you can give some format to your floatingButtonNew

        //And finally  add to the frameLayout the new View in this case the floatingButtonNew
        frameLayout.addView(floatingActionButtonNew);
    }
}

在这个视图声明的顺序中,您将再次从浮动按钮返回图像。因此,首先声明的视图将从第二个声明的视图返回,而实现顺序的关键是在以removeAllViews编程FrameLayout方式LinearLayout添加时再次添加此视图。

于 2018-11-17T23:55:34.617 回答
1

您还可以尝试使用ViewGroup.addViewInLayout()获取索引的方法的功能,确定视图应该位于列表的末尾还是开头

于 2011-12-13T07:46:30.787 回答
1

如果有问题的视图是按钮,则程序化调整需要单行 xml 来实现完整的 z 顺序控制。否则,setElevation()、setZ()、setTranslationZ() 和 BringToFront() 都会莫名其妙地让你失败。请参阅下面单独线程的描述。

https://stackoverflow.com/a/68674834/14116101

于 2021-08-06T00:34:49.760 回答
0

我要么将其可见性设置为 0,要么将其下方的视图置于其前面。

于 2015-07-28T22:25:51.327 回答
-5

你可以尝试做view.setVisibility(View.INVISIBLE)view.setVisibility(View.GONE)

更新:

向您展示了如何完成您想做的事情。

于 2011-07-20T20:33:25.707 回答