用户界面View之TextView(实现跑马灯效果)
来源:互联网 发布:淘宝零点抢购在哪里 编辑:程序博客网 时间:2024/06/16 20:31
向前吧,荡起生命之舟,不必依恋和信泊,破浪的船自会一路开放常新的花朵。
本讲内容:TextView 文本
并认识一下使用setMovementMethod()方法实现文本可滚动
示例一:
下面是布局文件:
<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" tools:context="com.example.text.MainActivity$PlaceholderFragment" > <TextView android:id="@+id/textViewId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textSize="30sp"/></RelativeLayout>
下面是主文件:
package com.example.text;import android.app.Activity;import android.os.Bundle;import android.text.method.ScrollingMovementMethod;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private TextView t1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);t1=(TextView) findViewById(R.id.textViewId);//设置移动方法 使用setMovementMethod()方法实现文本可滚动t1.setMovementMethod(ScrollingMovementMethod.getInstance());}}
下面是string.xml文件:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">Text</string> <string name="hello_world">曾经有一年春天, 我走过一棵树下,看着那树上的某一片叶子,我很喜欢。 等秋天时,在那秋风起中,我又来到这棵树下,但却找不到那我喜欢的绿叶,我以为,我找不到它了。 可是我却不知道,它还在那里,只是颜色改变,等我带着惆怅转身离开,秋风中那树上的一片枯叶被卷起, 在我身边环绕,伴随我一路,但我惆怅中还在思恋那曾经的绿叶,却不知,原来我要等的,要找的, 在我的身边.........</string> <string name="action_settings">Settings</string></resources>
示例二:实现跑马灯效果
下面是res/layout/activity_main.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true" android:scrollHorizontally="true" android:textSize="25sp" android:text="@string/hello_world"/></LinearLayout>
<!--激活焦点--> android:focusable="true" <!--单行显示--> android:singleLine="true" <!--这里设置为超出文本后滚动显示--> android:ellipsize="marquee" <!--这个是设置滚动几次,这里是无限循环--> android:marqueeRepeatLimit="marquee_forever" <!--TouchMode模式的焦点激活--> android:focusableInTouchMode="true" <!--横向超出后是否有横向滚动条--> android:scrollHorizontally="true"
示例三:如果有多个TextView要实现该效果,该方法显示不能满足,因为被第一个TextView抢得焦点,其余不能
public class MarqueeText extends TextView{public MarqueeText(Context context) {super(context);}public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public MarqueeText(Context context, AttributeSet attrs) {super(context, attrs);}public boolean isFocused() {return true;//使每个Tv都激活焦点}}
下面是res/layout/activity_main.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" > <com.example.tvtest.MarqueeText android:id="@+id/id_tv1" 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.tvtest.MarqueeText android:id="@+id/id_tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/id_tv1" android:ellipsize="marquee" android:focusable="true" android:layout_marginTop="10dp" android:focusableInTouchMode="true" android:singleLine="true" android:text="@string/hello_world" /></RelativeLayout>
Take some time and enjoy it
0 0
- 用户界面View之TextView(实现跑马灯效果)
- Android之TextView实现跑马灯的效果(笔记)
- Android之TextView实现跑马灯的效果(笔记)
- 自定义View—实现滚动TextView(跑马灯)效果
- TextView实现跑马灯效果
- TextView实现跑马灯效果
- TextView实现跑马灯效果
- TextView实现跑马灯效果
- TextView实现跑马灯效果
- TextView实现跑马灯效果
- TextView实现跑马灯效果
- TextView实现跑马灯效果
- TextView实现跑马灯效果
- textview实现跑马灯效果
- TextView实现跑马灯效果
- TextView实现跑马灯效果
- TextView 实现跑马灯效果
- TextView 实现跑马灯效果
- eax ...edi esp ebp寄存器简介(转)
- JSTL标签库_4_ cparam_cimport_credirect_curl
- sizeof用法小结
- 查看gcc/g++默认include路径
- redis主从的配置和使用
- 用户界面View之TextView(实现跑马灯效果)
- javascript 几句话快速找出字符串内指定字符的下标。
- Android网络编程之socket和http协议的区别
- 马的遍历问题
- linux下搭建SVN服务器
- Qt5编译时出现<QtGui/QApplication> ---> No such file or directory错误解决办法
- python os模块的学习
- 再按一次退出应用
- iOS Icon and Image Sizes 图标图片尺寸