Android TextView可以滚动,但不显示滚动条
我有一个带有多个选项卡的Android TextView可以滚动,但不显示滚动条,android,textview,scroll,scrollbar,Android,Textview,Scroll,Scrollbar,我有一个带有多个选项卡的TabHost,其中的内容被定义为FrameLayout,它包装了一个TextView,每个选项卡都有大量的文本,超出了屏幕布局的显示范围,因此我必须为每个选项卡启用垂直滚动 主要是这些选项卡是动态创建的(通过编程),因此我必须以这种方式指定所有选项: final SoftReference<TabHost> th = new SoftReference<TabHost>((TabHost) ((Activity) globvars.getCont
TabHost
,其中的内容被定义为FrameLayout
,它包装了一个TextView
,每个选项卡都有大量的文本,超出了屏幕布局的显示范围,因此我必须为每个选项卡启用垂直滚动
主要是这些选项卡是动态创建的(通过编程),因此我必须以这种方式指定所有选项:
final SoftReference<TabHost> th = new SoftReference<TabHost>((TabHost) ((Activity) globvars.getContext()).findViewById(android.R.id.tabhost));
final TabSpec setContent = th.get().newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
view.setBackground(globvars.getContext().getResources().getDrawable(R.drawable.rounded_edges));
view.setPadding(25, 25, 25, 25);
view.setTextColor(Color.WHITE);
view.setLines(50);
view.setMaxLines(maxLines);
view.setEllipsize(TextUtils.TruncateAt.START);
view.setHorizontalScrollBarEnabled(false);
view.setVerticalScrollBarEnabled(true);
view.setMovementMethod(new ScrollingMovementMethod());
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
view.setVerticalFadingEdgeEnabled(true);
view.setGravity(Gravity.BOTTOM);
view.setOverScrollMode(1);
view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 11);
view.setTypeface(Typeface.MONOSPACE);
return view;
}
});
----编辑(01-19-2014)-----
好的,根据海盗992号的回答,我终于可以让它工作了。我真正的错误是假设即使是创建选项卡的方法(如上所述)也会收到一个TextView
作为参数,在TabSpec
定义中使用视图将其转换为TextView
。因此,实际上,我并不知道我实际上是在视图上设置滚动条的(不知道视图类也没有提供配置滚动条的公共编程方法),所以我遵循海盗992
的建议,创建了一个包含以下内容的单独布局:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tabsContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:background="@drawable/rounded_edges"
android:gravity="bottom"
android:padding="8dp"
android:scrollHorizontally="false"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical"
android:textColor="#FFFFFF"
android:textSize="11sp"
android:typeface="monospace"
tools:ignore="SmallSp" />
如果在XML布局资源中没有专门启用滚动条,则基本视图类似乎不提供用于初始化滚动条的公共方法。但是,您没有理由不能在XML资源中定义选项卡内容,通过将android:scrollbars
属性设置为vertical
,启用垂直滚动条,并从TabContentFactory
动态充气
在您的案例中,类似这样的情况:
公共视图createTabContent(字符串标记){
活动活动=(活动)globvars.getContext();
TextView视图=(TextView)LayoutFlater.from(活动)。充气(R.layout.mytabcontent,
(ViewGroup)activity.findviewbyd(android.R.id.tabcontent),false;
view.setMovementMethod(新的ScrollingMovementMethod());
view.setText(“我的文本”);
返回视图;
}
我读对了吗,您没有使用实际的文本视图
,您使用的是框架布局
,然后铸造它?你为什么不使用文本视图
?我没有正确解释自己,你是对的。FrameLayout
实际上并不是转换成TextView
,而FrameLayout
将TextView
包装在里面,这实际上是我在代码中通过编程创建的。我在问题中改变了这一点。谢谢为什么要将TabHost
存储在SoftReference
中?如果此引用不在活动的范围内
,则应改用WeakReference
。在活动
被销毁后保留视图
是没有意义的,这样做还可以防止活动
被垃圾收集,因为视图
保存对其上下文的引用。另外,当从软引用或弱引用获取对象时,您应该检查null。@corsair992我刚刚定义了SoftReference
,为了方便GC的任务,这个应用程序有点复杂,有数百个线程引用TabHost
,因此,定义SoftReference
只是告诉GC,它可以在最后一次使用后的合理时间内释放。你是对的,我应该测试对象是否为null,我没有任何问题,但最好这样做,所以我会更改它。我还尝试了一个WeakReference
,但由于它释放得太早,导致了很多NP异常,所以我将它改为软引用
,效果很好。如果在获得TabHost
(在任何情况下都应该这样做)时检查null,那么保持对它的弱引用就可以了。依靠软引用将对象保留一段合理的时间是一种危险的做法;它在正常情况下可能会工作,但在内存密集使用时会失败。此外,由于软引用将在内存中保留整个视图
层次结构和活动
本身(以及它们持有引用的所有内容),因此可能会导致其他更重要的软引用对象被垃圾回收。谢谢,我将在今天或明天尝试这个方法,并对结果进行评论。它非常有效,我用解决方案更新了我的问题。非常感谢@NKN:您在选项卡内容布局中包含的LinearLayout
似乎没有任何作用;您应该将TextView
移动到XML布局的根目录下,以获得更轻量级的布局层次结构。问题也变了。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tabsContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:background="@drawable/rounded_edges"
android:gravity="bottom"
android:padding="8dp"
android:scrollHorizontally="false"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical"
android:textColor="#FFFFFF"
android:textSize="11sp"
android:typeface="monospace"
tools:ignore="SmallSp" />
final TabSpec setContent = th.get().newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
public View createTabContent(String tag) {
// tabs_content is the layout above
final View ll_view = LayoutInflater.from(globvars.getContext()).inflate(R.layout.tabs_content, null);
final TextView view = (TextView) ll_view.findViewById(R.id.tabsContent);
view.setMovementMethod(new ScrollingMovementMethod());
return ll_view;
}
});