在布局中剪一个洞或动态地遮挡视图的一部分。

3

这里输入图片描述

当Activity启动时,用户应该看到左侧

  • 即:顶部的绿色View被裁剪,只有其底部可见
  • 红色View以交互方式显示在下方,使得用户可以按下红色按钮等操作
  • 对于绿色View的顶部部分,任何触摸事件都不应起作用(除了顶部不可见之外)

当用户触摸绿色View的底部时,整个绿色View应变为可见状态

  • 那么,为什么我不能只为绿色View设置一个上边距(类似于制作此图片的方式)?原因是,绿色View具有特殊性质,它包含手势检测,并以高度依赖于其完整尺寸的方式响应这些手势——因此,在仍需要将其中一部分设为不可见(在用户触摸底部部分之前),我无法调整绿色View的大小。

有什么建议吗?

右侧的XML,如何获得不调整绿色View大小的左侧?

绿色View实际上不是TextView,而是一个具有基于手势的动画效果的自定义ViewGroup

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
    <RelativeLayout
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:background="@drawable/border_red">
            <Button
                    android:background="@drawable/btn_red"
                    android:text="View Below Visible"
                    android:textSize="20sp"
                    android:layout_centerInParent="true"
                    android:layout_width="260dp"
                    android:layout_height="60dp"/>
        </RelativeLayout>
        <TextView
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:background="@drawable/btn_green"
                android:text="View with Special Gesturing"
                android:textColor="@color/white"
                android:textSize="20sp"
                android:gravity="bottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </RelativeLayout>
</RelativeLayout>

从这些图片来看,你不能简单地将红色视图添加到绿色视图的顶部(仍然是全屏),高度计算出留出足够的空间来显示所需的绿色视图底部吗? - user
@Luksprog 很好的简单想法,但不幸的是它行不通,因为我还为根布局设置了一个背景,它覆盖整个屏幕,并且需要在图像中任何看到黑色的地方透过来 - 如果我只是把红色视图放在绿色视图前面,背景将不可见,因为它始终是 z-顺序的底部视图(在绿色视图下方)。 - Cel
1
以上的想法如何?可以简单地创建一个“切片”,作为红色视图的背景(根将只是一个容器),我的猜测是你只需要红色视图部分的背景,因为绿色填充整个屏幕(?!?)。这种方法的可行性取决于背景是什么,有多大等因素。 - user
@Luksprog 这是一个可能的解决方案,但背景本身会发生变化,实际上在绿色视图后面有许多红色视图,因此不是理想的解决方案 - 如果没有人提出负担其他视图较少的解决方案,我将尝试它.. 谢谢你的解决方案! - Cel
1个回答

1
将绿色视图放入一个LinearLayout中,并在其上方加入一个带权重的透明视图。类似以下代码:
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="7"/>
        <WhateverCustomViewGroup
            android:background="@drawable/btn_green"
            android:text="View with Special Gesturing"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:gravity="bottom"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>

将手势检测器附加到LinearLayout而不是绿色视图上,这样即使折叠时仍然可以响应。要展开,只需在空视图上调用setVisibility(GONE),绿色视图就会展开到其完整大小。

我认为这可能有效,但是我特别查看了我正在尝试使用的自定义ViewGroup,并且在触摸监听器之上它似乎依赖于自定义视图本身的大小 - 你有什么办法解决吗?否则,我需要一段时间来理解这个Curl ViewGroup的设置(特别是因为它使用OpenGL)。 - Cel
有趣。那么,当用户在绿色视图折叠时进行卷曲手势时,应该发生什么?我明白你所说的关于视图大小在这里起着重要作用,但我不确定你实际上想通过横幅大小的页面卷曲器实现什么。 - Geobits
当用户触摸横幅大小的页面卷曲器时,我将拍摄红色视图的快照,并将其设置为绿色视图的页面(实际上看起来是红色的)- 基本上我正在尝试使用这个特定的卷曲器来处理红色视图,而不是静态位图,不幸的是,这个卷曲器不是我可以添加子项的ViewGroup。 - Cel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接