我搜索并搜索了如何将一个垂直(或水平)滚动条添加到TextView的答案,而不必仅使用XML来添加该行:android:scrollbars =“vertical”。
必须有一种方法可以以编程方式执行此操作,而不需要在另一个ScrollView中粘贴它。
我刚刚发现了如何以及因为我对此感到兴奋,并希望帮助那些坚持同样问题的人,这里是:
答案 0 :(得分:4)
Rusian Yanchyshyn在Android: Enable Scrollbars on Canvas-Based View
的答案中贴出了关键字在一个无聊的班级的帮助下初始化程序块我们现在可以执行以下操作:
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();
}
}
答案 1 :(得分:1)
// try this
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = new TextView(this);
textView.setText("demotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotext");
textView.setVerticalScrollBarEnabled(true);
textView.setLines(3);
textView.setMovementMethod(new ScrollingMovementMethod());
addContentView(textView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
答案 2 :(得分:0)
仅在滚动时显示
Xml(在textView元素中):
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
OnCreate:
textView.movementMethod = ScrollingMovementMethod()
答案 3 :(得分:0)
首先 - 创建一个 style.xml
<style name="VerticalScrollableTextView">
<item name="android:scrollbars">vertical</item>
<item name="android:scrollbarFadeDuration">0</item>
</style>
第二个 - 使用 ContextWrapper
val textView = TextView(
ContextThemeWrapper(context, R.style.VerticalScrollableTextView)
)
textView.movementMethod = ScrollingMovementMethod.getInstance()
仅此而已!