简体   繁体   中英

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?. I am new to Android development so please help me to solve this.

On your outermost LinearLayout, try calling removeViewAt(0). This should remove the first LinearLayout. Then call addView() passing it the first 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. But for a smooth swapping you are going to need to learn about Translate Animation .

Here a working example of how to swap two views (eg two 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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