简体   繁体   English

如何交换两个 LinearLayout

[英]How to swap two LinearLayout

I have a parent Linear Layout inside which there are two Linear Layout and a button.Each Linear Layout inside the parent Linear layout has two items in it.我有一个父线性布局,其中有两个线性布局和一个按钮。父线性布局内的每个线性布局都有两个项目。 Items include a edit text and a spinner.what I want is to swap this two LinearLayout when i click the button.So my question is How do i do that programatically?.项目包括编辑文本和微调器。我想要的是在单击按钮时交换这两个 LinearLayout。所以我的问题是如何以编程方式执行此操作? I am new to Android development so please help me to solve this.我是 Android 开发的新手,所以请帮我解决这个问题。

On your outermost LinearLayout, try calling removeViewAt(0).在最外面的 LinearLayout 上,尝试调用 removeViewAt(0)。 This should remove the first LinearLayout.这应该删除第一个 LinearLayout。 Then call addView() passing it the first LinearLayout.然后调用 addView() 将第一个 LinearLayout 传递给它。

One simple way would be to just make two layouts.一种简单的方法是只制作两个布局。 Designed such that when you are ready to use the other one, switch layouts and then set that as the current view.设计为当您准备使用另一个布局时,切换布局,然后将其设置为当前视图。

Or you could do so with various animation classes if trying to get fancy with it.或者,如果您想喜欢它,您可以使用各种动画类来实现。 If there is an exact visual effect you are trying to receive, perhaps provide a bit more on what you want.如果您想要获得确切的视觉效果,也许可以提供更多关于您想要的内容。

As Karim mentioned, you can use setVisibilty() to View.GONE and the to View.VISIBLE.正如卡里姆提到的,你可以使用 setVisibilty() 到 View.GONE 和到 View.VISIBLE。 But for a smooth swapping you are going to need to learn about Translate Animation .但是为了顺利交换,您将需要了解Translate Animation

Here a working example of how to swap two views (eg two LinearLayouts):这是如何交换两个视图(例如两个 LinearLayouts)的工作示例:

ViewGroup root = findViewById(R.id.my_root);
// assumption: root has 2 child views only 
// swap left and right (or top and bottom)
View leftView = root.getChildAt(0);
root.removeViewAt(0);
root.addView(leftView);
// now the two child views of root are swapped

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM