本文實例為大家分享了單行文本水平觸摸滑動效果,通過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.VeVB.COm/201610/yuanma/Androidtouchmove(VeVB.COm).rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答