Android UI实现单行文本水平触摸滑动效果

 更新时间:2016年10月24日 11:33:08   作者:Qiaoidea  
这篇文章主要为大家详细介绍了Android UI实现单行文本水平触摸滑动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了单行文本水平触摸滑动效果,通过EditText实现TextView单行长文本水平滑动效果。
下一篇再为大家介绍 多行文本折叠展开效果,自定义布局View实现多行文本折叠和展开。

1.初衷

最近做应用的时候有用到TextView单行长文本,当文本内容过长时候又想实现触摸水平滑动效果。网上找了很多,都没有看到有效解决方案。

其中,看到最常见的也是最笨拙滴采用重写TextView并继承实现touch 和 Gesture手势。个人觉得很麻烦。

后来经提醒发现了其实最简单的方案:

直接使用 EditText 就好了。 因为EditText需要编辑和移动光标的缘故,使得它是可以水平滑动的。因此我们只需要设置其为透明背景,并且不可以获得焦点。

效果图:

2.具体实现

定义EditText,并设置背景透明,不能获得焦点。

android:background=”@android:color/transparent”
android:focusable=”false”

(注:不能使用 editable=“false”,因为这样就不能编辑滑动。而通过使用 focusable=”false” 同样不可编辑同时可以滑动)

3.补充

补充一点,要隐藏光标,只用设置

android:cursorVisible=”false”//隐藏

setCursorVisible(false);

代码示例:

<EditText
 android:id="@+id/tt"
 android:layout_width="0dip"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:layout_margin="15dip"
 android:background="@android:color/transparent"
 android:focusable="false"
 android:singleLine="true"
 android:cursorVisible="false"
 android:text="简易实现 TextView单行文本水平触摸滑动效果。简易实现 TextView单行文本水平触摸滑动效果。简易实现 TextView单行文本水平触摸滑动效果"
 android:textColor="#000"
 android:textSize="20dip" />

附上下载地址:http://xiazai.jb51.net/201610/yuanma/Androidtouchmove(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

最新评论