本文介绍了Android-如何将后续视图发送回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在LinearLayout中添加一些视图,顶部略有重叠,这是代码:

I'm adding some views to a LinearLayout with a slight overlap in the top, here is the code:

viewHolder.linearLayout.removeAllViews();
for (int i = 0; i < conversation.getPreviousMessages().length; i++) {
    View messageView = layoutInflater.inflate(R.layout.layout_previous_message_row, null);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(0, -5, 0, 0);

    viewHolder.linearLayout.addView(messageView, layoutParams);
}

在此代码中,一些视图以A,B,C的顺序添加到布局中,其中A放在后面,C放在前面.

In this code some views are added to layout in order A, B, C, with A to the back and C to the front.

我想颠倒顺序,将A放在前面,将C放在后面.

I would like to reverse the order, making A to the front and C to the back.

我尝试了,但没有任何反应.

I have tried this, but nothing happens.

我怎么能做到这一点?拜托,您能给我带来一些代码吗?

How can I reach this?Please, could you people bring me some code?

谢谢.

推荐答案

在墙上碰了几拳之后,我终于找到了解决方法:

After some punches against the wall, I finally found a solution:

1.-我将布局从LinearLayout更改为RelativeLayout,因为BringChildToFront在LinearLayout中具有怪异的行为.

1.- I changed the layout from being a LinearLayout to being a RelativeLayout because bringChildToFront has a weird behavior among LinearLayout.

2.-在将视图添加到RelativeLayout后,我向后遍历了这些视图,并创建了一个必带子ChildToFront.

2.- After adding the view to the RelativeLayout, I iterated backwards over the views and I made a bringChildToFront.

这是代码:

for (int i = 0; i < conversation.getPreviousMessages().length; i++) {
     View messageView = layoutInflater.inflate(R.layout.layout_previous_message_row, null);
     messageView.setId(99+i);

     viewHolder.relativeLayout.addView(messageView);

     if (i > 0) {
         RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
         layoutParams.setMargins(0, -5, 0, 0);
         layoutParams.addRule(RelativeLayout.BELOW, 99+i-1);

         messageView.setLayoutParams(layoutParams);
     }
 }

 for (int i = viewHolder.relativeLayout.getChildCount() - 1; i >= 0; i--) {
     viewHolder.relativeLayout.bringChildToFront(viewHolder.relativeLayout.getChildAt(i));
 }

我希望它能帮助某人.

这篇关于Android-如何将后续视图发送回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:41