如何管理,使一个孩子在另一条战线定制的ViewGroup? [英] How to manage to bring one child in front of other in custom viewgroup?

查看:135
本文介绍了如何管理,使一个孩子在另一条战线定制的ViewGroup?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建android系统我自己的自定义的ViewGroup。我有两个孩子。一个是一个的LinearLayout(它的第一个孩子,并涵盖了屏幕的一半)与一些背景图片和按钮,在它和另一种是扩展视图(它的第二个孩子,覆盖整个屏幕),我用我的手指画点什么。

I am creating my own custom viewgroup in android. I have two children. One is a linearLayout (it's the first child and covers half of the screen) with some background image and buttons over it and other is a extension of View (it's the second child and covers whole screen) where I draw something using my finger.

我想要的(第一个孩子),线性布局的(第二个孩子)下的视图扩展被隐藏,这样我可以用一些手势老二滑动到右侧(一种谷歌,YouTube上像幻灯片),并看到第一个孩子(的LinearLayout)。问题是在 onLayout 我把在特定的顺序,但孩子们的第一个孩子(的LinearLayout)永远是在前面,无论我做什么。

I want the (first child) Linear Layout to be hidden under the (second child) extension of view so that I can use some gesture to swipe the second child to the right hand side (kind of like slide of google,youtube) and see the first child (LinearLayout). The problem is inside onLayout I place the children in certain order but the first child (LinearLayout) always comes in front no matter what I do.

  secondchild.layout(0,0,top,bottom);
  firstchild.layout(0,0,top/2,bottom);

我也试过

  firstchild.layout(0,0,top/2,bottom);      
  secondchild.layout(0,0,top,bottom);

但第一个孩子永远是在上面。

But the first child always comes on top.

code为的ViewGroup服装:

Code for Costume ViewGroup:

public class RootViewLayout extends ViewGroup  {
      private View mDrawView;
      private View mSlideView;
      private int mTop;
      private int mDragRange;

      public RootViewLayout(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
      @Override
      protected void onFinishInflate() {
          mDrawView  = findViewById(R.id.content_frame_white);
          mSlideView = findViewById(R.id.slide_frame);       
     }
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         int widthSize = MeasureSpec.getSize(widthMeasureSpec);
         int heightSize = MeasureSpec.getSize(heightMeasureSpec);
         setMeasuredDimension(widthSize, heightSize);   
     }
     @Override
     protected void onLayout(boolean changed, int left, int top, int right, int bottom){
            bringChildToFront(mDrawView);
            mDrawView.layout(0, 0, right, bottom);
            mSlideView.layout(0, 0, right/2, bottom);   
     }
}

XML code:

XML Code :

<com.example.drawapp.RootViewLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/Root_View_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/white">
     <com.example.drawapp.DrawView
          android:id="@+id/content_frame_white"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/whitepaperwithcoffeestain">    
     </com.example.drawapp.DrawView>
     <LinearLayout 
          android:id="@+id/slide_frame"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:background="@drawable/slidebackgrd"
          android:orientation="vertical">
          <Button 
            android:id="@+id/pen"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:background="@drawable/pic"/>
      </LinearLayout>
</com.example.drawapp.RootViewLayout>

当我不把

   bringChildToFront(mDrawView);

空白放在适当的黑色背景,但是这不是我真正想要的。我想长满DrawView(其背景是白色的咖啡渍过的话)整个屏幕。

the blank is placed with proper black background, but that's not what I actually want. I want whole screen to be covered with DrawView (whose background is white with coffee stain over it).

有没有告诉孩子任何特定的方式放置一个在其他顶部?

Is there any specific way to tell the children to be placed one on top of other?

推荐答案

您需要更改的子视图的Z顺序。你应该使用<一个href=\"http://developer.android.com/reference/android/view/ViewGroup.html#bringChildToFront%28android.view.View%29\"相对=nofollow> bringChildToFront() ,即

You need to change the z-order of the child views. You should probably use bringChildToFront(), i.e.

parentLayout.bringChildToFront(secondChild);

不过,效果取决于家长的布局类型(例如,如果它是一个的LinearLayout 则意见将被交换)。既然你覆盖我猜想这意味着它是一个的RelativeLayout ,然后它应该工作,只要你想

However, the effect depends on the type of the parent layout (e.g. if it's a LinearLayout then the views would be swapped). Since you're overlaying I guess it means it's a RelativeLayout, and then it should work as you want.

我看到你的情况,你正在使用自定义的ViewGroup 。如果它只是实现了全宽/半宽孩子,那么我会建议它换为 RelativeLayout的。添加 secondchild match_parent 则firstChild 为中心的右0dp视图如调整宽度以半屏

I see that in your case you're using a custom ViewGroup. If it's only to achieve the "full width/half width" children, then I would suggest swapping it for a RelativeLayout. Add secondchild with match_parent and firstchild as right of a centered 0dp view as in Adjust width to half screen

或者另一种选择,可能更简单,就是只要改变上那张顶部(可见或消失)孩子的可视性。

Or another option, possibly simpler, is to just change the visibility on the child that goes on top (VISIBLE or GONE).

这篇关于如何管理,使一个孩子在另一条战线定制的ViewGroup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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