使TextView在Android上可滚动
我在textview中显示的文本似乎太长,无法容纳在一个屏幕中。 我需要使TextView可滚动。 我怎样才能做到这一点?
这是代码:
final TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.splash);
tv.setTypeface(face);
tv.setTextSize(18);
tv.setTextColor(R.color.BROWN);
tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
tv.setOnTouchListener(new OnTouchListener() {public boolean onTouch(View v, MotionEvent e) {Random r = new Random();int i = r.nextInt(101);if (e.getAction() == e.ACTION_DOWN) {tv.setText(tips[i]);tv.setBackgroundResource(R.drawable.inner);}return true;}
});
setContentView(tv);
#1楼
这是仅使用XML的“如何将ScrollBar应用于TextView”。
首先,您需要在main.xml文件中使用一个Textview控件,并向其中写入一些文本……就像这样:
<TextViewandroid:id="@+id/TEXT"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="@string/long_text"/>
接下来,将文本视图控件放在滚动视图之间,以显示该文本的滚动条:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_height="wrap_content"android:layout_width="fill_parent"><ScrollViewandroid:id="@+id/ScrollView01"android:layout_height="150px"android:layout_width="fill_parent"><TextViewandroid:id="@+id/TEXT"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="@string/long_text"/></ScrollView>
</RelativeLayout>
而已...
#2楼
当完成可滚动操作后,在视图中输入任何内容时,请将此行添加到视图的最后一行:
((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
#3楼
如果您不想使用EditText解决方案,那么以下方法可能会更好:
@Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.yourLayout);(TextView)findViewById(R.id.yourTextViewId).setMovementMethod(ArrowKeyMovementMethod.getInstance());
}
#4楼
这将提供带有滚动条的平滑滚动文本。
ScrollView scroller = new ScrollView(this);
TextView tv = new TextView(this);
tv.setText(R.string.my_text);
scroller.addView(tv);
#5楼
如果要在textview中滚动文本,则可以执行以下操作:
首先,您应该必须继承textview。
然后使用它。
以下是一个子类化textview的示例。
public class AutoScrollableTextView extends TextView {public AutoScrollableTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);setEllipsize(TruncateAt.MARQUEE);setMarqueeRepeatLimit(-1);setSingleLine();setHorizontallyScrolling(true);}public AutoScrollableTextView(Context context, AttributeSet attrs) {super(context, attrs);setEllipsize(TruncateAt.MARQUEE);setMarqueeRepeatLimit(-1);setSingleLine();setHorizontallyScrolling(true);}public AutoScrollableTextView(Context context) {super(context);setEllipsize(TruncateAt.MARQUEE);setMarqueeRepeatLimit(-1);setSingleLine();setHorizontallyScrolling(true);}@Overrideprotected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {if(focused)super.onFocusChanged(focused, direction, previouslyFocusedRect);}@Overridepublic void onWindowFocusChanged(boolean focused) {if(focused)super.onWindowFocusChanged(focused);}@Overridepublic boolean isFocused() {return true;}
}
现在,您必须以这种方式在XML中使用它:
<com.yourpackagename.AutoScrollableTextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="This is very very long text to be scrolled"/>
而已。
#6楼
上面“某人某处”( 使TextView在Android上可滚动 )的“专业提示”效果很好,但是,如果您正在向ScrollView动态添加文本,并且仅在用户位于时才希望在追加后自动滚动到底部,该怎么办? ScrollView的底部? (也许是因为如果用户向上滚动以阅读某些内容,则您不想在附加过程中自动重置到底部,这很烦人。)
无论如何,这里是:
if ((mTextStatus.getMeasuredHeight() - mScrollView.getScrollY()) <=(mScrollView.getHeight() + mTextStatus.getLineHeight())) {scrollToBottom();
}
使用mTextStatus.getLineHeight()可以做到,如果用户在ScrollView末尾的一行之内,则不要滚动到ToBottom()。
#7楼
使您的textview仅添加此
TextView textview= (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(new ScrollingMovementMethod());
#8楼
你可以
- 用
ScrollView
包围TextView
; 要么 - 将Movement方法设置为
ScrollingMovementMethod.getInstance();
。
#9楼
将此添加到您的XML布局:
android:ellipsize="marquee"
android:focusable="false"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="To Make An textView Scrollable Inside The TextView Using Marquee"
在代码中,您必须编写以下行:
textview.setSelected(true);
textView.setMovementMethod(new ScrollingMovementMethod());
#10楼
在XML的文本视图中添加以下内容。
android:scrollbars="vertical"
最后,添加
textView.setMovementMethod(new ScrollingMovementMethod());
在Java文件中。
#11楼
我为此奋斗了一个多星期,终于想出了如何使它工作的方法!
我的问题是,所有内容都将滚动为一个“块”。 文本本身正在滚动,但是滚动显示,而不是逐行显示。 这显然对我不起作用,因为它会切断底部的线条。 以前的所有解决方案都不适用于我,因此我自己制作了自己的解决方案。
到目前为止,这是最简单的解决方案:
在包内创建一个名为“ PerfectScrollableTextView”的类文件,然后将此代码复制并粘贴到:
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;public class PerfectScrollableTextView extends TextView {public PerfectScrollableTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);setVerticalScrollBarEnabled(true);setHorizontallyScrolling(false);}public PerfectScrollableTextView(Context context, AttributeSet attrs) {super(context, attrs);setVerticalScrollBarEnabled(true);setHorizontallyScrolling(false);}public PerfectScrollableTextView(Context context) {super(context);setVerticalScrollBarEnabled(true);setHorizontallyScrolling(false);}@Overrideprotected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {if(focused)super.onFocusChanged(focused, direction, previouslyFocusedRect);}@Overridepublic void onWindowFocusChanged(boolean focused) {if(focused)super.onWindowFocusChanged(focused);}@Overridepublic boolean isFocused() {return true;}
}
最后,以XML更改“ TextView”:
来自: <TextView
到: <com.your_app_goes_here.PerfectScrollableTextView
#12楼
您实际上不需要使用ScrollView
。
只需设置
android:scrollbars = "vertical"
布局的xml文件中TextView
属性。
然后使用:
yourTextView.setMovementMethod(new ScrollingMovementMethod());
在您的代码中。
宾果游戏,滚动!
#13楼
简单。 这是我的方法:
XML面:
<?xml version="1.0" encoding="utf-8"?> <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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" tools:context="com.mbh.usbcom.MainActivity"> <TextView android:id="@+id/tv_log" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:text="Log:" /> </RelativeLayout>
Java方面:
tv_log = (TextView) findViewById(R.id.tv_log); tv_log.setMovementMethod(new ScrollingMovementMethod());
奖金:
要使文本视图在文本填充时向下滚动,您必须添加:
android:gravity="bottom"
到TextView xml文件。 随着更多文本的输入,它将自动向下滚动。
当然,您需要使用append函数而非设置文本来添加文本:
tv_log.append("\n" + text);
我将其用于日志目的。
我希望这有帮助 ;)
#14楼
不必放入
android:Maxlines="AN_INTEGER"`
您只需添加以下内容即可完成工作:
android:scrollbars = "vertical"
然后,将以下代码放入您的Java类中:
textview.setMovementMethod(new ScrollingMovementMethod());
#15楼
下面的代码创建一个自动水平滚动textview:
在将TextView添加到xml时使用
<TextView android:maxLines="1" android:ellipsize="marquee"android:scrollHorizontally="true"/>
在onCreate()中设置TextView的以下属性
tv.setSelected(true);
tv.setHorizontallyScrolling(true);
#16楼
这样使用
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:maxLines = "AN_INTEGER"android:scrollbars = "vertical"
/>
#17楼
就我而言,约束布局,AS 2.3。
代码实现:
YOUR_TEXTVIEW.setMovementMethod(new ScrollingMovementMethod());
XML:
android:scrollbars="vertical"
android:scrollIndicators="right|end"
#18楼
将maxLines
和scrollbars
放在xml的TextView中。
<TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:scrollbars="vertical"android:maxLines="5" // any number of max line here./>
然后在Java代码中。
textView.setMovementMethod(new ScrollingMovementMethod());
#19楼
我在ScrollView
使用TextView
时遇到了这个问题。 这个解决方案对我有用。
scrollView.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {description.getParent().requestDisallowInterceptTouchEvent(false);return false;}});description.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {description.getParent().requestDisallowInterceptTouchEvent(true);return false;}});
#20楼
每当需要使用ScrollView作为父对象时 ,还可以将滚动移动方法与TextView一起使用。
当您纵向将设备横向放置时,会发生一些问题 。 (例如)整个页面都是可滚动的,但是滚动移动方法无效。
如果仍然需要使用ScrollView作为父级或滚动移动方法,则还可以使用以下desc。
如果您没有任何问题,请使用EditText代替TextView
见下文 :
<EditTextandroid:id="@+id/description_text_question"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@null"android:editable="false"android:cursorVisible="false"android:maxLines="6"/>
在这里,EditText的行为类似于TextView
您的问题将得到解决
#21楼
XML-您可以使用android:scrollHorizontally
属性
是否允许文本宽于视图(因此可以水平滚动)。
可以是布尔值,例如“ true”或“ false”。
Prigramacaly- setHorizontallyScrolling(boolean)
#22楼
尝试这个:
android:scrollbars = "vertical"
#23楼
在Kotlin中,用于使textview可滚动
myTextView.movementMethod= ScrollingMovementMethod()
并在xml中添加此属性
android:scrollbars = "vertical"
#24楼
真正必要的是setMovementMethod()。 这是使用LinearLayout的示例。
文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
<TextViewandroid:id="@+id/tv1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="@string/hello"/>
</LinearLayout>
文件WordExtractTest.java
public class WordExtractTest extends Activity {TextView tv1;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);tv1 = (TextView)findViewById(R.id.tv1);loadDoc();}private void loadDoc() {String s = "";for(int x=0; x<=100; x++) {s += "Line: " + String.valueOf(x) + "\n";}tv1.setMovementMethod(new ScrollingMovementMethod());tv1.setText(s);}
}
#25楼
我没有找到TextView滚动来支持“翻转”手势,该手势在向上或向下滑动后仍继续滚动。 我最终自己实现了它,因为由于各种原因我不想使用ScrollView,而且似乎没有MovementMethod允许我选择文本并单击链接。
#26楼
这就是我纯粹用XML做到的方式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><ScrollViewandroid:id="@+id/SCROLLER_ID"android:layout_width="fill_parent"android:layout_height="wrap_content"android:scrollbars="vertical"android:fillViewport="true"><TextViewandroid:id="@+id/TEXT_STATUS_ID"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1.0"/></ScrollView>
</LinearLayout>
笔记:
android:fillViewport="true"
与android:layout_weight="1.0"
android:fillViewport="true"
结合使用将使textview占用所有可用空间。定义Scrollview时,请勿指定
android:layout_height="fill_parent"
否则scrollview无效! (这使我刚才浪费了一个小时!FFS)。
专家提示:
要在添加文本后以编程方式滚动到底部,请使用以下命令:
mTextStatus = (TextView) findViewById(R.id.TEXT_STATUS_ID);
mScrollView = (ScrollView) findViewById(R.id.SCROLLER_ID);private void scrollToBottom()
{mScrollView.post(new Runnable(){public void run(){mScrollView.smoothScrollTo(0, mTextStatus.getBottom());}});
}
#27楼
我发现的最好方法:
使用具有以下这些额外属性的EditText替换TextView:
android:background="@null"
android:editable="false"
android:cursorVisible="false"
不需要包装在ScrollView中。
使TextView在Android上可滚动相关推荐
- 在Android上运行Windows XP
No rooting or custom modifications needed, we're going to do this with stock Android and a few free ...
- 如何在Android上使背景20%透明
本文翻译自:How to make a background 20% transparent on Android 我如何使Textview的背景大约20%透明(不完全透明),背景中是否有颜色(即白色 ...
- 有没有办法在Android上运行Python?
我们正在开发S60版本,该平台具有不错的Python API. 但是,关于Android上的Python尚无任何官方资料,但是由于Jython存在,有没有办法让蛇和机器人一起工作? #1楼 目前还没有 ...
- Android上百实例源码分析以及开源分析集合打包
感谢网友banketree的收集,压缩包的内容如下: 1.360新版特性界面源代码 实现了360新版特性界面的效果,主要涉及到Qt的一些事件处理与自定义控件.但源码好像是c++. 2.aidl跨进程调 ...
- Android上传文件到Web服务器,PHP接收文件(一)
Android上传文件到服务器,通常采用构造http协议的方法,模拟网页POST方法传输文件,服务器端可以采用JavaServlet或者PHP来接收要传输的文件.使用JavaServlet来接收文件的 ...
- Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读
术语和概念 屏幕尺寸 屏幕的物理尺寸,以屏幕的对角线长度作为依据(比如 2.8寸, 3.5寸). 简而言之, Android把所有的屏幕尺寸简化为三大类:大,正常,和小. 程序可以针对这三种尺 ...
- 基于glibc的程序在android上的移植
如何将Linux中现有的众多资源以最小代价移植到android是很多开发人员关注的,下面这篇文章讲了一个比较取巧的方法,我试验了一下,最基本的hello world可以跑,但是复杂的大型程序要考虑很多 ...
- android上使用蓝牙设备进行语音输入
主要实现步骤如下: 1.确保已经和蓝牙耳机配对连接上. 2.开启蓝牙信道 AudioManager mAudioManager = (AudioManager)getSystemService(Con ...
- android仿ios弹框_在“提示”框中:iOS外观(在Android上运行),Google Maps作为Time Machine,下载Wii游戏保存...
android仿ios弹框 Once a week we round up some great reader tips and share them with everyone. Read on t ...
最新文章
- jQuery 对象和 DOM 对象
- jupyter notebook python插件_VS Code Python 将支持 Jupyter Notebook
- 如何从零到一地开始机器学习?
- servlet html登录,Servlet实现用户登录
- python的gui界面 可视化_使用可视化设计窗体的GUI程序
- linux,下载与安装
- Moss/Sharepoint 为特定用户组设置特定视图以及可见视图,自定义列表新建页,修改页和显示页(无代码法)...
- 批处理写的关机小程序--bat
- js校验规则--去空格、加空格
- SVN移植、合库、分库
- python类概念是什么意思_python面向对象是什么意思?面向对象九大概念简介
- 深度学习群体行为识别python包_入门Python深度学习,学会这个方法事半功倍
- 平板无线网无法连接网络连接服务器,平板电脑可以连接无线网络但上不了网的解决方法...
- 便携式三星mysql_三星折叠手机终于来了!9 月 18 日正式发售
- matalab三维画图
- 团购网站的发展趋势分析
- python实现输入一个正整数_Python中实现输入一个整数的案例
- Python的列表、元组、字典
- Como criar uma linguagem usando ANTLR4 e Java
- Python课设实验 之 公交查询系统.(文本导入 字典树 储存.)
热门文章
- ubuntu下安装redis及在php中使用
- Download Microsoft Visual Studio 2010 Ultimate Trial - ISO from Official Microsoft Download Center
- Eclipse 生成 Jar 包
- day25 面向对象继承 多态
- Kafka到Hdfs的数据Pipeline整理
- 你真的会写二分查找吗
- chmod命令(chmod函数)自动清除设置用户ID位和设置组ID位
- Ubuntu在系统栏 给应用程序添加提示图标
- C言语教程第四章: 数组(4)
- [征询意见]更换Logo后的效果[dudu]