我在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);
这是仅使用XML的“如何将ScrollBar应用于TextView”。
首先,您需要在main.xml文件中使用一个Textview控件,并向其中写入一些文本……就像这样:
接下来,将文本视图控件放在滚动视图之间,以显示该文本的滚动条:
而已...
当完成可滚动操作后,在视图中输入任何内容时,请将此行添加到视图的最后一行:
((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
如果您不想使用EditText解决方案,那么以下方法可能会更好:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayout);
(TextView)findViewById(R.id.yourTextViewId).setMovementMethod(ArrowKeyMovementMethod.getInstance());
}
这将提供带有滚动条的平滑滚动文本。
ScrollView scroller = new ScrollView(this);
TextView tv = new TextView(this);
tv.setText(R.string.my_text);
scroller.addView(tv);
如果要在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);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
public void onWindowFocusChanged(boolean focused) {
if(focused)
super.onWindowFocusChanged(focused);
}
@Override
public boolean isFocused() {
return true;
}
}
现在,您必须以这种方式在XML中使用它:
而已。
上面“某人某处”( 使TextView在Android上可滚动 )的“专业提示”效果很好,但是,如果您正在向ScrollView动态添加文本,并且仅在用户位于时才希望在追加后自动滚动到底部,该怎么办? ScrollView的底部? (也许是因为如果用户向上滚动以阅读某些内容,则您不想在附加过程中自动重置到底部,这很烦人。)
无论如何,这里是:
if ((mTextStatus.getMeasuredHeight() - mScrollView.getScrollY()) <=
(mScrollView.getHeight() + mTextStatus.getLineHeight())) {
scrollToBottom();
}
使用mTextStatus.getLineHeight()可以做到,如果用户在ScrollView末尾的一行之内,则不要滚动到ToBottom()。
使您的textview仅添加此
TextView textview= (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(new ScrollingMovementMethod());
你可以
ScrollView
包围TextView
; 要么 ScrollingMovementMethod.getInstance();
。 将此添加到您的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());
在XML的文本视图中添加以下内容。
android:scrollbars="vertical"
最后,添加
textView.setMovementMethod(new ScrollingMovementMethod());
在Java文件中。
我为此奋斗了一个多星期,终于想出了如何使它工作的方法!
我的问题是,所有内容都将滚动为一个“块”。 文本本身正在滚动,但是滚动显示,而不是逐行显示。 这显然对我不起作用,因为它会切断底部的线条。 以前的所有解决方案都不适用于我,因此我自己制作了自己的解决方案。
到目前为止,这是最简单的解决方案:
在包内创建一个名为“ 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);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
public void onWindowFocusChanged(boolean focused) {
if(focused)
super.onWindowFocusChanged(focused);
}
@Override
public boolean isFocused() {
return true;
}
}
最后,以XML更改“ TextView”:
来自:
到:
您实际上不需要使用ScrollView
。
只需设置
android:scrollbars = "vertical"
布局的xml文件中TextView
属性。
然后使用:
yourTextView.setMovementMethod(new ScrollingMovementMethod());
在您的代码中。
宾果游戏,滚动!
简单。 这是我的方法:
XML面:
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);
我将其用于日志目的。
我希望这有帮助 ;)
不必放入
android:Maxlines="AN_INTEGER"`
您只需添加以下内容即可完成工作:
android:scrollbars = "vertical"
然后,将以下代码放入您的Java类中:
textview.setMovementMethod(new ScrollingMovementMethod());
下面的代码创建一个自动水平滚动textview:
在将TextView添加到xml时使用
在onCreate()中设置TextView的以下属性
tv.setSelected(true);
tv.setHorizontallyScrolling(true);
这样使用
就我而言,约束布局,AS 2.3。
代码实现:
YOUR_TEXTVIEW.setMovementMethod(new ScrollingMovementMethod());
XML:
android:scrollbars="vertical"
android:scrollIndicators="right|end"
将maxLines
和scrollbars
放在xml的TextView中。
然后在Java代码中。
textView.setMovementMethod(new ScrollingMovementMethod());
我在ScrollView
使用TextView
时遇到了这个问题。 这个解决方案对我有用。
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
description.getParent().requestDisallowInterceptTouchEvent(false);
return false;
}
});
description.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
description.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
每当需要使用ScrollView作为父对象时 ,还可以将滚动移动方法与TextView一起使用。
当您纵向将设备横向放置时,会发生一些问题 。 (例如)整个页面都是可滚动的,但是滚动移动方法无效。
如果仍然需要使用ScrollView作为父级或滚动移动方法,则还可以使用以下desc。
如果您没有任何问题,请使用EditText代替TextView
见下文 :
在这里,EditText的行为类似于TextView
您的问题将得到解决
XML-您可以使用android:scrollHorizontally
属性
是否允许文本宽于视图(因此可以水平滚动)。
可以是布尔值,例如“ true”或“ false”。
Prigramacaly- setHorizontallyScrolling(boolean)
尝试这个:
android:scrollbars = "vertical"
在Kotlin中,用于使textview可滚动
myTextView.movementMethod= ScrollingMovementMethod()
并在xml中添加此属性
android:scrollbars = "vertical"
真正必要的是setMovementMethod()。 这是使用LinearLayout的示例。
public class WordExtractTest extends Activity {
TextView tv1;
/** Called when the activity is first created. */
@Override
public 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);
}
}
我没有找到TextView滚动来支持“翻转”手势,该手势在向上或向下滑动后仍继续滚动。 我最终自己实现了它,因为由于各种原因我不想使用ScrollView,而且似乎没有MovementMethod允许我选择文本并单击链接。
这就是我纯粹用XML做到的方式:
笔记:
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());
}
});
}
我发现的最好方法:
使用具有以下这些额外属性的EditText替换TextView:
android:background="@null"
android:editable="false"
android:cursorVisible="false"
不需要包装在ScrollView中。