gpt4 book ai didi

android - 如何设法在自定义 View 组中将一个 child 带到另一个 child 面前?

转载 作者:太空狗 更新时间:2023-10-29 16:37:50 24 4
gpt4 key购买 nike

我正在 android 中创建自己的自定义 View 组。我有两个 child 。一个是 linearLayout(它是第一个 child ,覆盖了屏幕的一半),上面有一些背景图像和按钮,另一个是 View 的扩展(它是第二个 child ,覆盖了整个屏幕),我用手指画了一些东西。

我希望(第一个 child )线性布局隐藏在(第二个 child ) View 扩展下,这样我就可以使用一些手势将第二个 child 滑动到右侧(有点像谷歌的幻灯片,youtube ) 并查看第一个子项 (LinearLayout)。问题出在 onLayout 中,我按特定顺序放置 child ,但无论我做什么,第一个 child (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);

但是第一个 child 总是排在最前面。

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 代码:

<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 覆盖(其背景为白色,上面有咖啡渍)。

有没有具体的方法告诉 children 一个叠一个?

最佳答案

您需要更改 subview 的 z 顺序。您可能应该使用 bringChildToFront() ,即

parentLayout.bringChildToFront(secondChild);

但是,效果取决于父布局的类型(例如,如果它是 LinearLayout,那么 View 将被交换)。既然你在叠加,我猜这意味着它是一个 RelativeLayout,然后它应该可以按你想要的方式工作。

我看到在您的情况下您使用的是自定义 ViewGroup。如果它只是为了实现“全宽/半宽”子级,那么我建议将其换成 RelativeLayout。添加带有 match_parentfirstchildsecondchild 作为居中 0dp View 的右侧,如 Adjust width to half screen

或者另一种可能更简单的选择是仅更改位于顶部的子项的可见性(VISIBLE 或 GONE)。

关于android - 如何设法在自定义 View 组中将一个 child 带到另一个 child 面前?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24168362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com