Android TouchDelegate 扩大点击区域
来源:互联网 发布:如何开淘宝网店货源 编辑:程序博客网 时间:2024/05/17 03:32
用于更改View的触摸区域,在平常开发中遇到一个问题就是因为图标太小导致老是点击不中,用户体验不太好,TouchDelegate可以改变可点击区域的大小。
Layout如下
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="demo.cn.com.demo.SecondActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:orientation="horizontal" android:id="@+id/parent" android:weightSum="2"> <Button android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:text="Hello World !" /> <Button android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:text="Hello World !" /> </LinearLayout></RelativeLayout>
java code如下
package demo.cn.com.demo;import android.graphics.Rect;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.TouchDelegate;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second2); final LinearLayout parent = (LinearLayout) findViewById(R.id.parent); final Button btn1 = (Button) findViewById(R.id.btn1); Button btn2 = (Button) findViewById(R.id.btn2); parent.post(new Runnable() { @Override public void run() { Rect rect = new Rect(); btn1.getHitRect(rect); rect.top -= 100; rect.left -= 100; rect.bottom += 100; rect.right += 100; TouchDelegate touchDelegate = new TouchDelegate(rect,btn1); ((View)btn1.getParent()).setTouchDelegate(touchDelegate); } }); }}
此时btn1能够点击的区域就是下面的红线框区域
采用此方法需要注意:
1、若View的自定义触摸范围超出Parent的大小,则超出的那部分无效。
2、一个Parent只能设置一个View的TouchDelegate,设置多个时只有最后设置的生效。
3、如果点击区域延伸到了兄弟控件,那么还是兄弟控件响应。
0 0
- Android TouchDelegate 扩大点击区域
- 使用android.view.TouchDelegate扩大View的触摸点击区域
- 使用android.view.TouchDelegate扩大View的触摸点击区域
- Android笔记之利用TouchDelegate扩大View点击区域
- 使用TouchDelegate扩大View的点击区域
- 使用TouchDelegate扩大View的点击区域
- Android 扩大触摸的触发区域 TouchDelegate
- Android学习之 扩大控件点击触摸区域及TouchDelegate的介绍
- 用 TouchDelegate 扩大子 View 的点击区域
- Android 扩大点击区域
- android checkbox 扩大点击区域
- 使用 TouchDelegate 扩大控件的点击范围
- Android 扩大ImageView的点击区域
- Android 扩大ImageView的点击区域
- Android中 扩大ImageView的点击区域
- Android如何扩大按钮点击区域
- Android扩大图片的点击区域
- Android 扩大ImageView的点击区域
- ionic APP 版本升级策略(IOS&ANDROID)
- C++入门学习第一天:c++与c的区别
- UVALive 7040 (容斥)
- 监控RuntimeException
- Hibernate中解决懒加载的方法
- Android TouchDelegate 扩大点击区域
- 苹果制作U盘,重新安装操作系统
- laravel5-1 Study notes
- JVM栈
- MySQL5.6存储过程和函数
- 在RHEL7上安装服务器端的图形化界面
- Xshell+WinSCP在Linux上安装jdk
- JSP 的基本原理
- 欢迎使用CSDN-markdown编辑器