实现的onClick只为一个TextView复合绘 [英] Implement onClick only for a TextView compound drawable

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

问题描述

我需要有一些文字上的左侧绘制,我想执行一些code,当用户点击/触摸图像(仅图像,而不是文字),所以我用的的LinearLayout 的用的的TextView 的和的的ImageView 的是可以点击的,启动一个onclick事件。 XML解析器建议我用的的TextView 的替换此一的复合绘制的,这将得出同样的事情的 XML 的远不如线。我的问题是:我可以指定我想只的绘制处理onClick事件中的的TextView 的,而不是在的TextView 的本身呢?我已经看到了一些解决方案,包括编写自己的的TextView 的扩展,但我只能够在布局资源中做到这一点,如果可能的话感兴趣,否则我会继续下面的 XML 的code:

 <的LinearLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:比重=中心
        机器人:方向=横向>        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=底
            机器人:paddingTop =10dp
            机器人:paddingLeft =10dp
            机器人:paddingRight =10dp
            机器人:文字=@字符串/ home_feedback_title
            机器人:文字颜色=@机器人:彩色/ primary_text_dark
            机器人:文字样式=大胆
            机器人:paddingBottom会=4DP/>
        < ImageView的
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:SRC =@绘制/ action_feedback
            机器人:可点击=真
            安卓的onClick =onClickFeedback
            机器人:contentDescription =@字符串/ action_feedback_description/>
< / LinearLayout中>


解决方案

您可以去任何一种方式。使用复合绘是,虽然,因为它的目的是要成为一个优化速度更快。它使用较少的内存,因为你减少3次为1和,因为你失去1深度它的速度更快的布局。

如果我是你,我会考虑退一步,看看文本和图像截取触摸做任何动作可能是一件好事。一般来说具有更大的触摸区域可以更容易地preSS。有些用户可能实际上倾向于触摸文字,而不是图像。

最后,如果你去合并2的这条路线,你可能要考虑使用按钮,而不是的TextView 。您可以样式按钮,没有周围的矩形。他们称之为一个无国界的按钮。因为你得到了你一个可操作的项目点击视觉反馈,这是很好的地方为的ImageView 的TextView 通常不可操作的。

如何

创建Android的无边界按钮

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>

解决方案

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.

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.

How to Create Borderless Buttons in Android

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

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