让 TextView 自带滚动条
来源:互联网 发布:有手机用windows系统 编辑:程序博客网 时间:2024/04/30 23:17
在android上面让TextView 过多的文字实现有滚动条,之前想简单了以为设置TextView的属性就可以实现,结果还是需要ScrollView配合使用,才能达到滚动条的效果有两种方式实现,
一种是代码写java的layout
Java代码
- RelativeLayout.LayoutParams
param new= RelativeLayout.LayoutParams( 80,80); - //初始化滚动条控件
- ScrollView
scrollView new= ScrollView( this); - scrollView.setScrollContainer(true);
- scrollView.setFocusable(true);
- //初始化文字控件
- TextView
textView new= TextView( this); - textView.setHorizontallyScrolling
( true); - textView.setText("走情感路线,哈哈哈,\n
走情感路线,哈哈哈" ); -
- scrollView.addView(textView);
- main_view.addView(scrollView,
param);
另一种则用layout.xml来实现
Xml代码
- <ScrollView
xmlns:android="http://schemas.android.com/apk/res/android" -
android:layout_height="fill_parent" - android:scrollbars="vertical"
android:fadingEdge="vertical"> -
android:layout_width="fill_parent" android:layout_height="wrap_content" - android:id="@+id/text_view"
android:textColor="#071907" android:paddingTop="5dip" /> - </ScrollView>
=========================================================================================
一、Xml代码
- <TextView
- android:id="@+id/textview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:singleLine="false"
- android:maxLines="5"
- android:scrollbars="vertical"
- />
二、还需要在代码中设置 TextView 相应的方法
- TextView textView = (TextView)findViewById(R.id.text_view);
- textView.setMovementMethod(ScrollingMovementMethod.getInstance());
好了,大功告成。
附:
顺便讲下 TextView 自动滚动的实现方法,下面介绍两种方法:
一、在代码中实现:
textView .setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView .setSingleLine(true);
textView .setMarqueeRepeatLimit(6);
二、在XML中实现:
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:singleLine="true"
android:text="dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
android:marqueeRepeatLimit="marquee_forever" android:ellipsize="marquee"
android:scrollHorizontally="true" android:width="150dip"></TextView>
一切OK,当 textView 获取焦点后,就会自动滚动。
- 让TextView 自带滚动条
- 让TextView 自带滚动条
- 让 TextView 自带滚动条
- 让 TextView 自带滚动条
- 让TextView 自带滚动条
- 让TextView 自带滚动条
- 让 TextView 自带滚动条
- 让TextView 自带滚动条
- 让TextView 自带滚动条
- 让TextView 自带滚动条
- 让 TextView 自带滚动条
- android 让TextView自带滚动条
- 带滚动条TextView
- 设置带滚动条的TextView
- Android设置带滚动条的TextView
- 设置带滚动条的TextView
- 设置带滚动条的TextView
- 让TextView 有滚动条的效果
- FastReport 4.13.1 中文菜单显示不全或者乱码解决方法
- Android_文件存储
- 百钱百鸡:公鸡5元一只,母鸡3元一只,小鸡1元3只,100元要买100只鸡,共有几种情况
- memset ,memcpy 和strcpy 的根本区别
- arcgis rest服务无法访问
- 让 TextView 自带滚动条
- C#冒泡排序
- 仙道录 第一卷 天道门 第一章 无名村
- android广播事件处理二
- JavaScript Patterns 中文版
- struts2-----3
- 获得焦点事件和失去焦点事件
- bastard RIL 协议
- SQL PARTITION BY的用法