Android view getVisibility()的值

(1)0    --------   VISIBLE     可见
(1)4    --------   INVISIBLE    不 可见但是占用布局空间
(1)8    --------   GONE    不 可见也不占用布局空间
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,如果想要实现 View 穿透效果,可以使用以下两种方式: 1. 使用 WindowManager 可以使用 WindowManager 来添加一个可以穿透的 View,将其添加到 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY 类型的窗口中,并设置 WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,这样就可以让该 View 穿透其他 View 响应触摸事件。 示例代码: ``` // 创建一个可以穿透的 View View view = new View(context); view.setBackgroundColor(Color.TRANSPARENT); WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSPARENT); WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); windowManager.addView(view, layoutParams); ``` 2. 使用 ViewGroup 在布局文件中使用一个继承自 ViewGroup 的自定义 View,重写 dispatchTouchEvent 方法,将事件分发给子 View 和自身,这样就可以实现子 View 穿透效果。 示例代码: ``` public class TouchThroughViewGroup extends ViewGroup { public TouchThroughViewGroup(Context context) { super(context); } public TouchThroughViewGroup(Context context, AttributeSet attrs) { super(context, attrs); } public TouchThroughViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); if (child.getVisibility() == View.VISIBLE) { child.dispatchTouchEvent(ev); } } super.dispatchTouchEvent(ev); return true; } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); child.layout(l, t, r, b); } } } ``` 在布局文件中使用该自定义 View: ``` <com.example.TouchThroughViewGroup android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView"/> </com.example.TouchThroughViewGroup> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值