无聊之中学习,好好玩
目的 :在屏幕上方推送消息,一直无限循环推送
一 实验效果图:
1.单行:
2.多行:
二:实验步骤:
1.修改helloword
效果图:
2.让其单行显示,显示成跑马灯效果:
效果图:
3.再加一个TextView:
效果图:
结果:
第一行是跑马灯效果,第二行不是,第二行整行显示。
4.创建一个类:
修改包名:
效果图:
三。核心代码:
<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.liu1.OtherText
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.liu1.OtherText
android:layout_below="@id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:layout_marginTop="10dp"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/hello_world" />
</RelativeLayout>
2.OtherTextView.java
<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.liu1.OtherText
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.liu1.OtherText
android:layout_below="@id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:layout_marginTop="10dp"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/hello_world" />
</RelativeLayout>
package com.example.liu1;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class OtherText extends TextView{
public OtherText(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public OtherText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public OtherText(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean isFocused(){
return true;
}
}
3 string.xml
<resources>
<string name="app_name">Liu1</string>
<string name="action_settings">Settings</string>
<string name="hello_world">我是刘鲁南,我是刘鲁南,我是刘鲁南,我是刘鲁南,我是刘鲁南,大好人。</string>
</resources>
四。总结:
单行跑马灯还是很容易的,多行跑马灯如果用前面的思路直接导致聚焦在第一行,第二行不动,看视频看出建一个类,里面有很多东西也没有搞懂,特别是add东西的时候
android:ellipsize="marquee" 跑马灯
android:focusable="true" 聚焦
android:focusableInTouchMode="true" 设置焦点联系方式
android:singleLine="true" 单行显示