Android 如何在自定义视图组中将一个子项置于其他子项之前?
我正在android中创建自己的自定义视图组。我有两个孩子。一个是线性布局(它是第一个孩子,覆盖了屏幕的一半),上面有一些背景图像和按钮,另一个是视图的扩展(它是第二个孩子,覆盖了整个屏幕),我用手指画东西 我希望(第一个孩子)线性布局隐藏在视图的(第二个孩子)扩展下,这样我就可以使用一些手势将第二个孩子滑动到右侧(有点像谷歌、youtube的幻灯片),然后看到第一个孩子(线性布局)。问题出在“内部”onLayout我将孩子按一定顺序排列,但无论我做什么,第一个孩子(LinearLayout)总是排在前面Android 如何在自定义视图组中将一个子项置于其他子项之前?,android,android-layout,layout,Android,Android Layout,Layout,我正在android中创建自己的自定义视图组。我有两个孩子。一个是线性布局(它是第一个孩子,覆盖了屏幕的一半),上面有一些背景图像和按钮,另一个是视图的扩展(它是第二个孩子,覆盖了整个屏幕),我用手指画东西 我希望(第一个孩子)线性布局隐藏在视图的(第二个孩子)扩展下,这样我就可以使用一些手势将第二个孩子滑动到右侧(有点像谷歌、youtube的幻灯片),然后看到第一个孩子(线性布局)。问题出在“内部”onLayout我将孩子按一定顺序排列,但无论我做什么,第一个孩子(LinearLayout)总
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);
但是第一个孩子总是排在第一位
服装视图组的代码:
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代码:
<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>
空白处有适当的黑色背景,但这不是我真正想要的。我想用DrawView覆盖整个屏幕(背景是白色的,上面有咖啡色)
是否有任何特定的方法告诉子视图将一个放置在另一个之上?您需要更改子视图的z顺序。您可能应该使用,即 但是,效果取决于父布局的类型(例如,如果它是
线性布局
,则视图将被交换)。既然你是在叠加,我想这意味着它是一个相对论,然后它应该可以按照你的意愿工作
我发现您使用的是自定义的视图组
。如果它只是为了实现“全宽/半宽”子项,那么我建议将其替换为RelativeLayout
。将secondchild
与match_parent
和firstchild
一起添加到居中的0dp视图右侧,如中所示
或者另一个可能更简单的选择是,只更改位于顶部的子对象的可见性(可见或消失)。我会带来ChildToFront(mDrawView);在Costude Viewgroup类中,现在我看不到第一个孩子,但我的第二个孩子被切成两半,另一半是空的:(@goonda请发布布局xml的相关部分(或者,如果添加带有代码的视图,则发布该部分)。DrawView是扩展视图的类,因为您有一个自定义视图组,bringChildToFront()可能已“损坏”语义学。让我检查一下。我尝试了不同的方法,但仍然不起作用:(,在你说现在我知道了z顺序。我不知道。
bringChildToFront(mDrawView);
parentLayout.bringChildToFront(secondChild);