反转LinearLayout的方向 [英] Reverse the direction of a LinearLayout

查看:339
本文介绍了反转LinearLayout的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何反转线性布局的方向?

How can I reverse the direction of a linear layout?

例如,如果我的布局是[view1,view2,view3],我想创建[view3,view2,view1].我的目标是从一种XML布局中增加一个左手实例和一个右手实例.

for example, if my layout is [view1, view2, view3] I want to create [view3, view2, view1]. My goal is to inflate a left and a right handed instances from one XML layout.

推荐答案

通常,我会说您不能使用标准SDK来执行此操作.但是,您可以创建一个方法,将所有子视图从LinearLayout中移出,将它们从LinearLayout中删除,然后以相反的顺序将它们重新添加回去.

Generally I would say you cannot do this using the standard SDK. You could however create a method which gets all of the subviews out of the LinearLayout, removes them from the LinearLayout, and then adds them back in reverse order.

LinearLayout ll = // inflate
ArrayList<View> views = new ArrayList<View>();
for(int x = 0; x < ll.getChildCount(); x++) {
    views.add(ll.getChildAt(x));
}
ll.removeAllViews();
for(int x = views.size() - 1; x >= 0; x--) {
    ll.addView(views.get(x));
}

这篇关于反转LinearLayout的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆