前言
我们在开发中经常会遇到一个小问题. 比如下面一个小例子:
这个文字太长, 单行中导致无法全部显示出来, 这就是今天要实现的功能. 当然, 百度中也有很多这种解决方案.
其中有一种, 例如:
- <TextView
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:singleLine="true"
- Android:ellipsize="marquee"
- Android:focusable="true"
- Android:focusableInTouchMode="true"
- Android:text="@string/hello_world" />
- Android:ellipsize="marquee"
- Android:focusable="true"
- Android:focusableInTouchMode="true"
xml 中添加这 3 行 就能实现效果了.
这种方法确实可以实现.
事实上开发过程中, 布局是非常复杂和多变的, 并不是我们一个 TextView 就能解决所有的布局和要求.
例如, 现在用两个 TextView
- <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=".MainActivity">
- <TextView
- Android:id="@+id/textview1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:ellipsize="marquee"
- Android:focusable="true"
- Android:focusableInTouchMode="true"
- Android:singleLine="true"
- Android:text="@string/hello_world" />
- <TextView
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:layout_below="@id/textview1"
- Android:ellipsize="marquee"
- Android:layout_marginTop="20dp"
- Android:focusable="true"
- Android:focusableInTouchMode="true"
- Android:singleLine="true"
- Android:text="@string/hello_world" />
- </RelativeLayout>
这个简单的功能就满足不了了.
第一个跑马灯效果没问题, 第二个就没实现了, 平常开发中, 两个 TextView 都解决不了, 平常开发中更解决不了了.
这就是今天我所要讲的内容.
首先, 我们先建一个类, 继承 TextView 这个类.
- package com.example.marqueetextview;
- import Android.content.Context;
- import Android.util.AttributeSet;
- import Android.view.ViewDebug.ExportedProperty;
- import Android.widget.TextView;
- public class MarqueeText extends TextView{
- public MarqueeText(Context context) {
- super(context);
- }
- public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- public MarqueeText(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- @ExportedProperty(category = "focus")
- public boolean isFocused() {
- return true;
- }
- }
这个时候实现 TextView 中的一个方法, isFocused(), 返回改成 return true.
然后进 xml 中 把 TextView 修改成我们自定义的这个控件.
- <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=".MainActivity">
- <com.example.marqueetextview.MarqueeText
- Android:id="@+id/textview1"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:ellipsize="marquee"
- Android:focusable="true"
- Android:focusableInTouchMode="true"
- Android:singleLine="true"
- Android:text="@string/hello_world" />
- <com.example.marqueetextview.MarqueeText
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"
- Android:layout_below="@id/textview1"
- Android:layout_marginTop="20dp"
- Android:ellipsize="marquee"
- Android:focusable="true"
- Android:focusableInTouchMode="true"
- Android:singleLine="true"
- Android:text="@string/hello_world" />
- </RelativeLayout>
然后我们再看一下效果.
已经全部实现成功了.
那这到底是为什么呢? 奥秘就在我们重载的 isFocused() 这个函数. return true 全部强制 Focused. 都有焦点了, 就都能实现了
如果没设置, 焦点都第一个, 第二个就无法实现.
这个是我写的 Demo:https://pan.baidu.com/s/1M1TghCh_R3kFnReM4li1VQ https://pan.baidu.com/s/1M1TghCh_R3kFnReM4li1VQ
来源: https://www.cnblogs.com/MrChen-/p/10310728.html