android - TextView可以滚动但不显示滚动条

标签 android textview scroll scrollbar

我有一个带有多个选项卡的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;
  }
});

使用这种方法,我确实可以向后和向前滚动,但在视觉上不会显示滚动条。我的意思是这个酒吧:

Virtual Scroll Bar

我错过了什么吗?滚动条是否必须由命令式自定义可绘制对象定义?

谢谢!

------ 编辑 (12-31-2013) ------

我已经四处寻找,但仍然找不到任何解决方案。我已经尝试了尽可能多的参数组合,但没有结果。特别是,我尝试过 this并且还将 FrameLayout 包装在 ScrollView 中,但不是在 TextView 本身显示滚动条,而是整个 TextView > 被包裹在滚动条中,并随着缓冲区变得越来越大而增长,这不是我想要的。

感谢任何帮助!

------ 编辑 (01-17-2014) ------

好吧,在这一点上,我可以保证我已经尝试了任何合乎逻辑的步骤(好吧,至少对我来说)来使这项工作成功,但仍然不行!我澄清一下,我还有大约 7-8 个额外 Activity ,而且滚动其中任何一个 Activity 都没有问题。不过,我只是在这个中使用 TabHost 。所以我开始对此进行悬赏,因为这已经危及我的精神理智。

我在下面发布我的布局:

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fondo_imagen"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

  <LinearLayout
            android:id="@+id/TabContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="99"
            android:orientation="vertical">
    <TabHost
            android:id="@+android:id/tabhost"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
      <LinearLayout
            android:id="@+id/TabLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        <!-- Horizontal scrolling for the tab widget -->
        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:scrollbars="none">
          <TabWidget
            android:id="@+android:id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        </HorizontalScrollView>
        <FrameLayout
            android:id="@+android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp" />
      </LinearLayout>
    </TabHost>
  </LinearLayout>

  <!-- Some additional LinearLayouts that don't have anything to do with the tab widget -->
  ...
</LinearLayout>

------ 编辑 (01-19-2014) ------

好的,根据 corsair992 的回答,我终于可以正常工作了。我真正的错误是假设即使创建选项卡的方法(上面发布的)也接收 TextView 作为参数,并在 TabSpec 中使用 View > 定义会将其转换为 TextView。所以事实上,我并不知道我实际上是在 View 上设置滚动条(不知道 View 类没有提供公共(public)编程方法来配置滚动条)两者都没有),所以我遵循了 corsair992 的建议并使用以下内容创建了一个单独的布局:

<?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" />

现在,我不再调用上述将所有这些属性设置为 View 的方法,而是简单地 inflate 此布局并设置 MovementMethod >:

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;
  }
});

最佳答案

如果未在 XML 布局资源中专门启用滚动条,则基本 View 类似乎不会提供用于初始化滚动条的公共(public)方法。但是,您没有理由不能在 XML 资源中定义选项卡内容,通过将 android:scrollbars 属性设置为 vertical 来启用垂直滚动条,然后将其展开动态地从 TabContentFactory 获取。

您的情况是这样的:

public View createTabContent(String tag) {
    Activity activity = (Activity) globvars.getContext();
    TextView view = (TextView) LayoutInflater.from(activity).inflate(R.layout.mytabcontent,
            (ViewGroup) activity.findViewById(android.R.id.tabcontent), false);
    view.setMovementMethod(new ScrollingMovementMethod());
    view.setText("My Text");
    return view;
}

关于android - TextView可以滚动但不显示滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20844374/

相关文章:

html - 使用主页滚动条滚动 div

java - 使用 Gson 时如何绕过循环引用?

android - 保存到 sd 卡的图像不会出现在 Android 图库应用中

android - 为什么 AutoCompleteTextView 的名字这么奇怪?

java - 抽屉导航标题 textview nullpointerexception

css - JavaFX 2.2 - 如何在 scrollPane 内缩放后调整滚动?

java - 从 ftp 服务器下载文件

java - 无法实现自定义ArrayList的RecycleAdapter获取数据库随机查询结果

java - 如何在 Java 中将@写成字符串

html - 卷轴底部的大空间