java - 在布局中切一个洞或动态遮挡部分 View

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

enter image description here

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

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

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

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

有什么建议吗?

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

绿色 View 实际上不是一个 TextView,而是一个带有手势驱动动画的自定义 View 组

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

最佳答案

将绿色 View 包裹在 LinearLayout 中,并在其上方放置一个加权透明 View 。像这样的东西:

    <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 而不是绿色 View ,因此它在折叠时仍然可以响应。要展开,只需在空 View 上setVisibility(GONE),绿色 View 就会展开到完整大小。

关于java - 在布局中切一个洞或动态遮挡部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12847041/

相关文章:

android - 如何在 android 的 subview 的 subview 中设置 z 索引?

java - 如何从java中的mysql数据库获取图表数据?

java - 无法使用 TomCat 将 MySQL Connector/J 与 Servlet 一起使用

java - 如何限制 bundle 在 OSGi 中对服务的使用?

java - 从 Web 服务解析 Sax

android - 在运行时检查互联网连接

Android布局渲染无需编译

android - ListView 行样式 - 文本左对齐,图标右对齐

android - 超薄库不支持带有多个 TextView 的标题

java - 不幸的是应用程序在调试时停止了