动态文本视图滚动条不显示



我已经动态创建了一个文本视图,并希望使其可滚动。

final RelativeLayout.LayoutParams params = parseLayoutParams(
frameMargins, context);
tv.setText(Utility.getpropertyString(controls.getText()));
final String textColor = Utility.getpropertyString(controls.getTextcolor());
tv.setTextColor(Color.parseColor(textColor));
tv.setTextSize(12);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setTextSize(tSize);
tv.setEllipsize(TextUtils.TruncateAt.END);
tv.setMaxLines(controls.getMaxlines());
tv.setTag(controls.getTagId());
tv.setLayoutParams(params);
tv.setEllipsize(TextUtils.TruncateAt.END);
tv.setVisibility(controls.getVisibility());
tv.setVerticalScrollBarEnabled(isScrollable);
tv.setScroller(new Scroller(context));
tv.setMovementMethod(new ScrollingMovementMethod());         
tv.setScrollBarFadeDuration(0);

但是我不滚动或滚动时无法在文本视图中看到滚动条。请帮忙!

从 Api 21 开始,视图滚动条可见性仅保留给 xml 布局,因为在将变量作为参数传递时,由于出现问题TypeArray删除了名为initializaScrollBars的重要函数。

因此,要以编程方式完成所需的操作,您可以像这样执行此操作。

创建名为scrolltextview.xml的 xml 布局

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>

现在,以编程方式创建它

TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.scrolltextview, null, false);
// now decorate it as your needs
tv.setText(m);
tv.setTextColor(Color.BLUE);
tv.setTextSize(23);
...
tv.setMovementMethod(new ScrollingMovementMethod());
// this is needed only if you want to show scrollbar also when text is not scrolling
tv.setScrollBarFadeDuration(0);
// thecontainer = the layout you want to add your new textview
thecontainer.addView(tv);

正如 Ferran 所指出的,initializeScrollBars()已被删除。有关错误报告及其删除的理由,请参阅此处。据我所知,没有其他严格的编程方法来为视图指定 scollbars。所有路径都通过 XML。:-(

我认为费兰的回答是一个很好的方法:它有效,易于理解,并且应该易于记录。但是,还有其他方法可以通过编程方式创建带有滚动条的TextView,并带有样式的轻微帮助。

适用于 API 21 及更高版本
定义一个名为"ViewWithScrollBars"的样式,如下所示:

<style name="ViewWithScrollbars">
<item name="android:scrollbars">vertical</item>
</style>

现在,我们可以使用TextView的四参数构造函数来应用样式。

TextView tv = new TextView(this, null, 0, R.style.ViewWithScrollbars);

此方法将创建一个带有滚动条的文本视图。但是,至少有一个警告。使用单个参数创建文本视图

new TextView(Context)

构造函数通过添加其他参数的其他构造函数进行伸缩。其中一个构造函数定义如下:

public TextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.textViewStyle);
}

第三个参数com.android.internal.R.attr.textViewStyle是一个 Android 内部样式,它将从主题中选取默认的textViewStyle。我建议的调用对第三个参数使用零,因此不会应用主题中定义的任何textViewStyle

解决此问题的合理方法可能是执行以下操作:

tv = new TextView(this, null, android.R.attr.textViewStyle, R.style.ViewWithScrollbars);

不幸的是,如果在主题中定义了第三个参数(defStyleAttr),则不使用第四个参数(defStyleRes)。因此,滚动条将不会显示。

如果您使用textViewStyle,则必须进行调整或仅使用以下方法。

适用于所有接口
使用上面的样式"ViewWithScrollBars",我们可以定义一个ContextThemeWrapper,它将滚动条安装到将用于创建文本视图的主题中。

ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.ViewWithScrollbars); // "this" is the Activity
tv = new TextView(ctw);

我向您推荐克里斯·贝恩斯(Chris Banes)的一篇题为"主题与风格"的文章,该文章解释了主题叠加的工作原理。

下面把所有这些放在一起。

主要活动.java

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = new TextView(this);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// This will actually work for API 21 and above.
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.ViewWithScrollbars);
tv = new TextView(ctw);
} else {
tv = new TextView(this, null, 0, R.style.ViewWithScrollbars);
}
tv.setText(R.string.lorem);
tv.setTextColor(Color.parseColor("red"));
tv.setTextSize(12);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
tv.setEllipsize(TextUtils.TruncateAt.END);
tv.setMaxLines(7);
tv.setTag(View.generateViewId());
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(params);
tv.setEllipsize(TextUtils.TruncateAt.END);
tv.setVisibility(View.VISIBLE);
tv.setVerticalScrollBarEnabled(true);
tv.setScroller(new Scroller(this));
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setScrollBarFadeDuration(0);
((RelativeLayout) findViewById(R.id.layout)).addView(tv);
}

}

最新更新