Android TextView文字横向自动滚动(跑马灯)
public class AlwaysMarqueeTextView extends TextView {
public AlwaysMarqueeTextView(Context context) {
super(context);
}
public AlwaysMarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isFocused() {
return true;
}
在布局XML文件中加入这么一个AlwaysMarqueeTextView,这个加入方法也是刚刚学的。
android:id=“@+id/AMTV1″
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:lines=“1″
android:focusable=“true”
android:focusableInTouchMode=“true”
android:scrollHorizontally=“true”
android:marqueeRepeatLimit=“marquee_forever”
android:ellipsize=“marquee”
android:background=“@android:color/transparent”
/>
ellipsize属性
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)
marqueeRepeatLimit属性
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。
focusable属性
自己猜测的,应该是能否获得焦点,同样focusableInTouchMode应该是滑动时能否获得焦点。
组合View的问题:
xmlns:android =“http://schemas.android.com/apk/res/android”
android:orientation =“vertical”
android:gravity =“center_vertical”
android:background =“@drawable/f_background”
android:layout_width =“fill_parent”
android:focusable =“true”
android:layout_height =“50px” >
< TextView
android:id =“@+id/info_text”
android:focusable =“true”
android:layout_width =“fill_parent”
android:layout_height =“wrap_content”
android:text =“test marquee .. “
android:textColor =“@color/black”
android:singleLine =“true”
android:ellipsize =“marquee”
android:marqueeRepeatLimit =“3″
android:textSize =“18sp”
/>
< TextView
android:id =“@+id/date_text”
android:layout_width =“fill_parent”
android:layout_height =“wrap_content”
android:layout_gravity =“bottom”
android:textColor =“@color/gray”
android:text =“2010/05/28″
android:textSize =“12sp”
/>
</ LinearLayout >
上面示例中2个TextView组合为一个View,由于设置了LinearLayout为focusable而TextView就没法取得焦点了,这样 这个TextView的跑马灯效果就显示不出来,就算你也设置TextView的 android:focusable=
"true"
也是 没用的. 这个时候就要使用addStatesFromChildren 这个属性了,在LinearLayout中设置这个属性,然后设置TextView的focusable=
"true"
就可以了.关于 addStatesFromChildren的说明:
Sets whether
this
ViewGroup's drawable states
also include its children's drawable states.
相关文章
- Android开发之搜索框自动提示细节MatrixCursor学习
- Android 使用SystemBarTint设置状态栏颜色
- 【黑马Android】(01)android简介/工程目录结构/拨号器/四种点击事件/后台偷偷发短信
- Android studio 提示“android qemu-system-i386.exe停止工作”
- Android MVP 详解
- Android中垃圾回收日志信息
- 《android开发艺术探索》读书笔记(十五)--Android性能优化
- Android面试涨薪攻略指南:Android面试必知必会Java知识点
- Android Studio修行手册,快速涨薪-【界面预览】、【自带截图】、【自动排序代码】、【快速查看界面】和【预览列表】、【Null判断】
- android java 堆栈_Android下打印调试堆栈方法
- Android 9.0 10.0 去掉开机提示 android is starting 的对话框
- 【Android 逆向】IDA 工具使用 ( 同步指定的 IDA View 视图 | Hex View 数据格式 | 过滤设置 )
- 【Android 应用开发】Android 开发环境下载地址 -- 百度网盘 adt-bundle android-studio sdk adt 下载
- 查看android源码版本号
- Android的ViewAnimator而它的子类ViewSwitcher-android学习之旅(三十三)
- android系统查看 gcc glibc和交叉编译版本信息
- 【Android-Jetpack进阶】7、DataBinding 布局的变量与事件绑定、inlclude 二级页面绑定、自定义 BindingAdapter
- Android的报错提示:Failed to resolve: com.android.support:appcompat-v7:28.0.0
- Android Studio的报错提示:android.content.res.Resources$NotFoundException: Resource ID #0xfffffe6c
- Android的java代码如何自动转Kotlin
- Android 9.0/P(android p指安卓9.0版本) okhttp3网络请求出错
- Android开发详解之App升级程序一点通