仅为 TextView 复合可绘制对象实现 onClick [英] Implement onClick only for a TextView compound drawable

查看:17
本文介绍了仅为 TextView 复合可绘制对象实现 onClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在左侧有一些带有可绘制对象的文本,并且我想在用户单击/触摸图像时执行一些代码(只有图像,而不是文本),所以我使用了 LinearLayout 带有一个 TextView 和一个可点击的 ImageView 并启动一个 onClick 事件.XML 解析器建议我用带有 compound drawableTextView 替换它,这样可以用更少的 XML 行来绘制相同的东西..我的问题是我可以指定我只想在 TextView 的可绘制对象上而不是在 TextView 本身上处理 onClick 事件吗?我已经看到了一些解决方案涉及编写自己的 TextView 扩展,但我只对能够在布局资源中执行此操作感兴趣,如果可能的话,否则我将保留以下 XML代码:

I need to have some text with a drawable on the left and I want to execute some code when the user clicks/touches the image (only the image, not the text), so I used a LinearLayout with a TextView and an ImageView which is clickable and launches an onClick event. The XML parser suggests me to replace this with a TextView with a compound drawable, which would draw the same thing with far less lines of XML.. My question is "can I specify I want to handle an onClick event only on the drawable of the TextView and not on the TextView itself? I've seen some solutions which involves writing your own extension of TextView, but I'm only interested in being able to do it within the layout resource, if possible, otherwise I'll keep the following XML code:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:paddingTop="10dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="@string/home_feedback_title"
            android:textColor="@android:color/primary_text_dark"
            android:textStyle="bold" 
            android:paddingBottom="4dp"/>


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/action_feedback" 
            android:clickable="true"
            android:onClick="onClickFeedback"
            android:contentDescription="@string/action_feedback_description"/>
</LinearLayout>

推荐答案

你可以选择任何一种方式.使用复合可绘制对象更快,因为它旨在进行优化.它使用更少的内存,因为您将 3 个视图减少到 1 个,并且布局更快,因为您失去了 1 个深度.

You can go either way. Using the compound drawable is faster though because it was intended to be an optimization. It uses less ram because you reduce 3 views into 1 and it's faster layout because you lose 1 depth.

如果我是你,我会考虑退后一步,看看文本和图像是否都拦截了触摸以执行任何操作可能是一件好事.一般来说,触摸区域越大,越容易按压.有些用户实际上可能更倾向于触摸文本而不是图像.

If I were you I'd consider stepping back to see if both the text and the image intercepting the touch to do whatever action is possibly a good thing. In general having a larger touch region makes it easier to press. Some users may actually be inclined to touch the text instead of the image.

最后,如果您采用合并 2 的路线,您可能需要考虑使用 Button 而不是 TextView.您可以设置按钮的样式,使其周围没有矩形.他们称之为无边框按钮.这很好,因为您会获得视觉反馈,即您单击了一个可操作的项目,而 ImageViewTextView 通常是不可操作的.

Lastly if you go that route of merging the 2 you might want to consider using a Button instead of a TextView. You can style the button to not have the rectangle around it. They call it a borderless button. It's nice because you get visual feedback that you clicked on a actionable item where as an ImageView or TextView normally aren't actionable.

如何在 Android 中创建无边框按钮

这篇关于仅为 TextView 复合可绘制对象实现 onClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆