gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:24:20 26 4
gpt4 key购买 nike

我有一个包含多个选项卡的 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

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

谢谢!

------ 编辑(2013 年 12 月 31 日)------

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

感谢任何帮助!

------ 编辑(2014 年 1 月 17 日)------

好吧,在这一点上,我可以保证我已经尝试了任何合乎逻辑的步骤(好吧,至少对我来说)来完成这项工作,但仍然不能!我澄清说我还有大约 7-8 个额外的 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>

------ 编辑(2014 年 1 月 19 日)------

好的,根据 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com