Android TextView实现水平滚动显示效果
来源:互联网 发布:centos php版本切换 编辑:程序博客网 时间:2024/05/16 09:08
方法一、xml设置属性+程序中获取焦点
第一步,首先在xml文件中设置TextView的属性:
<TextView android:id="@+id/text" android:layout_width="340dp" android:layout_height="65dp" android:layout_gravity="center_horizontal" android:gravity="center" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" />
ellipsize=”marquee”表示滚动显示;
focusable=”true”表示可以获取焦点;
focusableInTouchMode=”true”表示点击屏幕的上的某个控件时,不要立即执行相应的点击逻辑,而是先显示焦点(即控件被选中),再点击才执行逻辑。focusable与focusableInTouchMode的具体区别可参考 android:focusable和android:focusableInTouchMode的区别;
marqueeRepeatLimit=”marquee_forever”表示无限次循环显示,也可以赋上想要的循环次数;
singleLine=”true”表示只显示一行。这行一定要加,虽然现在singleLine已经被弃用,笔者也尝试用lines=”1”和maxLines=”1”代替,但均不能实现滚动效果。
第二步,在程序中获取焦点
...TextView displayText = (TextView).findViewById(R.id.text);displayText.setSelected(true); // 获取焦点displayText.setText("想要滚动显示的内容");...
经过以上两步就可以实现跑马灯式显示效果。
方法二、自定义MarqueeTextView
第一步,自定义MarqueeTextView类,并重写isFocused()方法返回true,让MarqueeTextView一直处于获取焦点状态。
public class MarqueeTextView extends TextView { public MarqueeTextView(Context context) { super(context); } public MarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr){ super(context, attrs, defStyleAttr); } @Override public boolean isFocused(){ return true; }}
第二步, 在xml文件里使用MarqueeTextView,记得要加包名:
<包名.MarqueeTextView android:id="@+id/text" android:layout_width="340dp" android:layout_height="65dp" android:layout_gravity="center_horizontal" android:gravity="center" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" />
此时就无需再调用setSelected(true)方法获取焦点了。在程序中设置显示内容:
...MarqueeTextViewdisplayText = (MarqueeTextView).findViewById(R.id.text);displayText.setText("想要滚动显示的内容");...
阅读全文
0 0
- Android TextView实现水平滚动显示效果
- Android开发之TextView文字水平滚动效果实现
- Android TextView实现滚动效果
- android组件TextView实现字体水平滚动
- android实现TextView垂直或水平滚动
- Android组件TextView实现字体水平滚动
- android实现TextView垂直或水平滚动
- android实现TextView垂直或水平滚动
- android textview 水平滚动
- Android textview和listview实现水平自动滚动的走马灯效果
- TextView实现滚动显示的效果
- TextView 实现走马观灯 水平滚动效果
- android中TextView中如何设置水平滚动效果
- android中TextView中如何设置水平滚动效果
- android中TextView中如何设置水平滚动效果
- android中TextView中如何设置水平滚动效果
- 如何实现textview多余文字的水平滚动显示
- android 用textview实现文字滚动效果
- 解决父元素高度塌陷的2种方法
- angularjs 路由使用html5风格 具体解决方案
- java.lang.IllegalStateException:Web app root system property already set to different value 错误原因及解决
- mysql 源码安装
- PAT 1001. 害死人不偿命的(3n+1)猜想
- Android TextView实现水平滚动显示效果
- win7系统鼠标指针乱动怎么办?
- 【专题】树形动态规划
- expected a component class,got[object object]
- SSM下的jdbc.properties文件配置
- 242. Valid Anagram
- Linux 常用命令
- Android---理解Application类
- Linux命令中su与sudo的详细区别