如何显示在一个TextView滚动条编程 [英] How to show scrollbars on a TextView programmatically
问题描述
我找啊找找到一个答案,如何在垂直(或水平)滚动条添加到一个TextView,而不必使用XML只是为了添加一行:机器人:滚动条=垂直
I have searched and searched to find an answer to how to add a vertical (or horizontal) scrollbar to a TextView without having to use the XML just to add the line: android:scrollbars="vertical".
必须有一个办法做到这一点编程,不需要另一个滚动型中坚持这一点。
There has to be a way to do this programmatically that doesn't require sticking this within another ScrollView.
我刚刚发现了如何和因为我的方式来兴奋这一点,并希望能帮助别人谁被套牢了同样的问题,那就是:
I've just found out how and because I am way to excited about this and want to help anyone else who is stuck with the same question, here it is:
推荐答案
Rusian Yanchyshyn,张贴在<一个在他的回答的关键href=\"http://stackoverflow.com/questions/4869276/android-enable-scrollbars-on-canvas-based-view/8582996\">Android:启用画布基础观滚动条
Rusian Yanchyshyn, posted the key in his answer at Android: Enable Scrollbars on Canvas-Based View
随着anonmous类和放大器的帮助;一个初始化块,我们现在可以做到以下几点:
With the help of an anonmous class & an initializer block we can now do the following:
TextView textViewWithScrollBars = new TextView(context)
{
{
setVerticalScrollBarEnabled(true);
setMovementMethod(ScrollingMovementMethod.getInstance());
setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
// Force scrollbars to be displayed.
TypedArray a = this.getContext().getTheme().obtainStyledAttributes(new int[0]);
initializeScrollbars(a);
a.recycle();
}
}
这篇关于如何显示在一个TextView滚动条编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!