android学习15#--简单自定义跑马灯textView控件

来源:互联网 发布:sql修改列默认值 编辑:程序博客网 时间:2024/06/06 12:36

有两种方法可以实现文字的跑马灯效果,第一种是借用textView的控件属性。第二种是自定义view。

利用控件属性实现跑马灯

singleLine属性

android:singleLine=”true”设置文本行为单行模式,如果不设或为flase,文本内容超过文本宽时会自动换行

ellipsize属性

android:ellipsize=”start” 省略号在开头
android:ellipsize=”middle” 省略号在中间
android:ellipsize=”end” 省略号在结尾
android:ellipsize=”marquee” 跑马灯显示

focusableInTouchMode和focusable

这两个属性我也不知道怎么讲,实际用的时候这两个属性都要同时设置为ture,才能实现焦点,先看下我从网上拷过来的分析:http://zhidao.baidu.com/link?url=iXLCeN8W6xCH1fMeHDhMVzA3_kbNlx9OLarVH5DwMcAuighven9wVszxSMBq-lbeTdUFfvsKs7w5QW0aQkuezAos4TcWoXpsrtPS5Uzm_sO。
要理解这个属性,首先你得知道,Android不是只面向手机的,它还有可能被安装在电视等非触摸输入设备上.即使是在手机上,目前很多手机也都支持键盘输入了.
focusable这种属性,更多的是为了解决非触摸输入的,因为你用遥控器或键盘点击控件,就必然要涉及到焦点的问题,只有可以获得焦点的控件才能响应键盘或者遥控器或者轨迹球的确定事件.
然后再反过来看focusableInTouchMode.这个属性的意思一如字面所述,就是在进入触摸输入模式后,该控件是否还有获得焦点的能力.
可以简单的理解为,用户一旦开始通过点击屏幕的方式输入,手机就进入了”touch mode”.
focusableInTouchMode这种属性,多半是设给EditText这种即使在TouchMode下,依然需要获取焦点的控件.(请自行想下EditText为什么在touch mode下也需要焦点).
比如Button之类的控件,在touch mode下,就已经没有获取焦点的必要了.

例子

只有同时将上面的属性设置为true,其中ellipsize为marquee才能实现跑马灯效果。
xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.uudou.study.rollingapplication.MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:ellipsize="marquee"            android:singleLine="true"            android:focusable="true"            android:focusableInTouchMode="true"            android:textSize="32sp"            android:text="这是一个跑马灯程序:习近平强调,南海诸岛自古以来就是中国领土" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:ellipsize="marquee"            android:singleLine="true"            android:focusable="true"            android:focusableInTouchMode="true"            android:textSize="32sp"            android:text="这是一个跑马灯程序:习近平强调,南海诸岛自古以来就是中国领土" />    </LinearLayout></RelativeLayout>

效果图:
这里写图片描述

总结

看效果图,为什么第二行没有跑马灯的效果。这是因为只能有一个控件设置为焦点,现在焦点被第一个抢走了,自然第二textView的焦点设置就无效了。解决此问题,只能自定义一个新的textView。

简单的自定义跑马灯textView控件

xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.uudou.study.textrollingapplication.MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <com.uudou.study.textrollingapplication.RollingTextView            android:layout_height="wrap_content"            android:layout_width="match_parent"            android:ellipsize="marquee"            android:singleLine="true"            android:textSize="32sp"            android:text="这是一个跑马灯程序:习近平强调,南海诸岛自古以来就是中国领土"/>        <com.uudou.study.textrollingapplication.RollingTextView            android:layout_height="wrap_content"            android:layout_width="match_parent"            android:ellipsize="marquee"            android:singleLine="true"            android:textSize="32sp"            android:text="这是一个跑马灯程序:习近平强调,南海诸岛自古以来就是中国领土"/>    </LinearLayout></RelativeLayout>

自定义textView源码:

public class RollingTextView extends TextView {    public RollingTextView(Context context) {        super(context);    }    public RollingTextView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public RollingTextView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    public boolean isFocused() {        return true;    }}

效果图:
这里写图片描述

总结

很明显第二行也有了跑马灯的效果,进一步证明了第一个例子的第二行没有获取到焦点。本例重写了isFocused()方法,返回true,使得每个控件都被设为焦点。

0 0
原创粉丝点击