我已经动态创建了一个textview,并希望使其可滚动。
<ContentPage ...
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core">
<StackLayout Margin="20">
<ListView ios:ListView.GroupHeaderStyle="Grouped">
...
</ListView>
</StackLayout>
</ContentPage>
但是当不滚动或滚动时,我无法在textview中看到滚动条。请帮忙!
答案 0 :(得分:1)
正如Ferran所指出的, initializeScrollBars()已被删除。有关错误报告及其删除的原理,请参见here。据我所知,没有其他严格的编程方式可以为视图指定滚动条。所有路径都通过XML。 :-(
我认为Ferran的答案是一个不错的选择:它有效,易于理解并且应该易于记录。但是,还有其他一些方法可以通过样式创建带有滚动条的 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);
此方法将创建带有滚动条的 TextView 。但是至少有一个警告。用单个参数创建 TextView 时
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 ,则必须进行调整或仅使用以下方法。
对于所有API
使用上面的样式“ ViewWithScrollBars”,我们可以定义一个ContextThemeWrapper
,它将把滚动条安装到主题中,该主题将用于创建 TextView 。
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.ViewWithScrollbars); // "this" is the Activity
tv = new TextView(ctw);
我请您参考克里斯·巴内斯(Chris Banes)的文章,标题为"Theme vs Style",其中解释了主题叠加层的工作原理。
以下内容将所有这些放在一起。
MainActivity.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);
}
}
答案 1 :(得分:0)
Api 21之前
要在TextView
中显示滚动条,可以使用此
// Force scrollbars to be displayed.
TypedArray a = this.getContext().getTheme().obtainStyledAttributes(new int[0]);
initializeScrollbars(a);
a.recycle();
但是,从Api 21开始,此方法受到保护并标记为@removed。 因此,访问此方法的唯一方法是使用反射
final RelativeLayout.LayoutParams params = parseLayoutParams(
frameMargins, context);
// **************************************
try {
Method method = View.class.
getDeclaredMethod("initializeScrollbars", TypedArray.class);
if (method != null) {
method.setAccessible(true);
TypedArray arr = obtainStyledAttributes(R.styleable.View);
method.invoke(tv, arr);
arr.recycle();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// *******************************************
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);