当前位置: 首页 > news >正文

网站源码查询discuz论坛官网

网站源码查询,discuz论坛官网,网站建设 项目要求,高端网站建设 杭州文章目录 Android 扩大View的点击区域使用padding属性使用TouchDelegate使用getLocationOnScreen监听 Android 扩大View的点击区域 使用padding属性 通过设置 padding 属性扩大点击区域。 使用#xff1a; ?xml version1.0 encodingutf-8? ?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:gravitycenter_horizontalandroid:orientationverticalTextViewandroid:idid/text1android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:padding20dpandroid:texthello /TextViewandroid:idid/text2android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:padding40dpandroid:textworld //LinearLayout使用TouchDelegate TouchDelegate 类是 Android 中的一个辅助类可以用于扩展 View 的触摸区域如小按钮。 目标View必须有父 View。给多个 View 扩大点击区域时不能是同一个父 View从 View 类的源码中可知设置 setTouchDelegate 时会覆盖之前的。 扩展类 fun View.expandTouchView(expandSize: Int 15.dp) {val parentView parent as? ViewparentView?.post {val rect Rect()getHitRect(rect)Log.e(TAG, rect $rect)rect.left - expandSizerect.top - expandSizerect.right expandSizerect.bottom expandSizeLog.e(TAG, expandRect $rect)parentView.touchDelegate TouchDelegate(rect, this)} }使用 val text1 findViewByIdTextView(R.id.text1) val text2 findViewByIdTextView(R.id.text2)text1.run {expandTouchView()setOnClickListener {ToastUtils.show((it as TextView).text.toString())} }text2.run {expandTouchView(40.dp)setOnClickListener {ToastUtils.show((it as TextView).text.toString())} }使用getLocationOnScreen监听 使用 getLocationOnScreen() 可以获取目标 View 在屏幕中的坐标。RectF 是一个用于表示浮点坐标的矩形区域的类可以用于设置扩大区域。通过监听 onTouchEvent() 触摸方法获取触摸的坐标然后判断是否在扩大区域内。 自定义View class ParentTouchView JvmOverloads constructor(context: Context, attrs: AttributeSet? null, defStyleAttr: Int 0 ) : LinearLayout(context, attrs, defStyleAttr) {private lateinit var text1: TextViewoverride fun onFinishInflate() {super.onFinishInflate()if (childCount 0) {text1 findViewById(R.id.text1)text1.setOnClickListener {ToastUtils.show(text1.text.toString())}}}override fun onTouchEvent(event: MotionEvent?): Boolean {event?.let { e -if (e.action MotionEvent.ACTION_DOWN) {if (isExpandView(text1, e.rawX, e.rawY, 100.dp)) {text1.performClick()}}}return super.onTouchEvent(event)}/*** 是否在View的扩大区域** param targetView 目标View* param touchX 点击的位置* param touchY 点击的位置* param expandSize 扩大区域的大小* return*/private fun isExpandView(targetView: View,touchX: Float,touchY: Float,expandSize: Int 15.dp): Boolean {// 获取目标View的Rectval rect RectF()val location IntArray(2)// 获取目标View的坐标targetView.getLocationOnScreen(location)val childX location[0].toFloat()val childY location[1].toFloat()rect.set(childX,childY,childX targetView.width,childY targetView.height)// 设置扩大区域后的Rectrect.apply {left - expandSizetop - expandSizeright expandSizebottom expandSize}// 判断是否在扩大区域内return rect.contains(touchX, touchY)} }使用 ?xml version1.0 encodingutf-8? com.example.xxx.view.ParentTouchView xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:gravitycenterandroid:orientationverticaltools:context.clickarea.LocationOnScreenActivityTextViewandroid:idid/text1android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:texthello //com.example.xxx.view.ParentTouchView
http://www.w-s-a.com/news/971/

相关文章:

  • php网站源码架构谷歌站群系统
  • 潮州网站seowordpress 调用置顶文章
  • 做带会员后台的网站用什么软件旅游网站建设资金请示
  • 商品网站怎么做wordpress 表情拉长
  • 商城网站设计费用网络公司怎样推广网站
  • 视频公司的网站设计工图网
  • 免费快速网站十八个免费的舆情网站