【问题标题】:Cut a hole in a layout or dynamically occlude part of a view在布局中挖洞或动态遮挡视图的一部分
【发布时间】:2012-10-11 19:26:19
【问题描述】:

当活动开始时,用户应该看到左侧

  • 即顶部的绿色视图被裁剪,只有底部可见
  • 下面的红色视图以交互方式可见,例如,用户可以按下红色按钮
  • 并且在绿色视图的顶部注册的任何触摸事件都应该不起作用(顶部的顶部不可见)

当用户触摸绿色视图的底部时,整个绿色视图应该变得可见

  • 现在为什么我不能只为绿色视图设置一个上边距(类似于生成此图像的方式) - 原因是绿色视图在它包含手势检测并响应这些手势的意义上是特殊的以高度依赖于其完整尺寸的方式 - 所以我无法调整绿色视图的大小,而我仍然需要使其一部分不可见(在用户触摸底部之前)

有什么建议吗?

右侧的 XML,如何在不调整绿色视图大小的情况下获取左侧

绿色视图实际上并不是一个 TextView,而是一个自定义视图组,其外观带有手势驱动的动画

<?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>

【问题讨论】:

  • 从这些图像来看,你不能简单地将红色视图添加到绿色视图(仍将是全屏)的顶部,并以这样的方式计算高度,以便留出足够的空间显示所需的绿色视图底部?
  • @Luksprog 非常简单的想法,但不幸的是它不会起作用,因为我还为根布局设置了背景,它覆盖了整个屏幕并且需要在图像中看到黑色的任何地方发光 -如果我只是将红色视图放在绿色视图前面,则背景将不可见,因为它始终是 z 顺序的底部视图(绿色视图下方)
  • 如何使用上面的想法,简单地创建一个普通背景的“切片”,用作红色视图的背景(在这种情况下,根只是一个容器),我的猜测是您只需要红色视图部分的背景,因为绿色填充了所有屏幕(?!?)。这项工作取决于背景是什么,有多大等。
  • @Luksprog 这是一个可能的解决方案,但背景本身会发生变化,实际上绿色视图后面有许多红色视图,因此不是理想的解决方案 - 如果没有人提出会影响其他解决方案的解决方案,请尝试观看次数减少.. 感谢您的解决方案!

标签: java android android-layout user-interface android-animation


【解决方案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),绿色视图将展开到其完整大小。

【讨论】:

  • 我认为这可能有效,但是查看了我试图在触摸侦听器it seems to rely on the size of the custom view itself 之上专门使用的自定义视图组 - 你有没有办法解决这个问题? - 否则我需要一段时间才能了解这个 curl 视图组是如何设置的(尤其是当它使用 opengl 时)..
  • 有趣。那么,当用户在绿色视图折叠时做出卷曲手势时,究竟应该发生什么?我看到您所说的视图大小在这里发挥了重要作用,但我不确定您实际尝试使用横幅大小的卷页器来完成什么。
  • 用户触摸横幅大小的卷页器的那一刻,我将拍摄红色视图的快照并将其设置为绿色视图的页面(实际上看起来是红色的) - 基本上我试图使用这个特殊的卷发器来处理红色视图而不是静态位图,不幸的是,卷发器不是一个视图组,我可以将子级添加到..
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多