一,TextView内容可滚动且有滚动条
参考智能课件,PPTRichView
private void initView(Context context) {
// setBackgroundResource(R.drawable.layer_shade_shape);
textView = new TextView(context);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (!TextUtils.isEmpty(textType) && textType.equalsIgnoreCase(PPTConstants.TYPE_MSOTEXTAREA) && !isAiAssistant) {
//版本判断是为了解决通过java代码的方式实现可滚动的TextView不显示滚动条的问题,重点是用了ViewWithScrollbars
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
ContextThemeWrapper ctw = new ContextThemeWrapper(getContext(), R.style.ViewWithScrollbars);
textView = new TextView(ctw);
} else {
textView = new TextView(getContext(), null, 0, R.style.ViewWithScrollbars);
}
textView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
textView.setVerticalScrollBarEnabled(true);
textView.setScrollBarFadeDuration(2000);
textView.setVerticalFadingEdgeEnabled(false);
textView.setSingleLine(false);
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
textView.setLayoutParams(layoutParams);
addView(textView);
}
二,java代码解析设置drawable的xml文件
参考智能课件PPTRichView.java
layer_shade_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="@color/ppt_transparent" />
</shape>
</item>
<item>
<shape>
<solid android:color="@color/ppt_transparent" />
</shape>
</item>
</layer-list>
java代码
private void setRootBackground(String textBackgroundColor, float ratio) {
try {
LayerDrawable layerDrawable = (LayerDrawable) getBackground();
//先还原样式,因为可能有多个PPTRichView用了layer_shade_shape,导致样式已经被设置
layerDrawable.setLayerInset(1, 0, 0, 0, 0);
GradientDrawable firstDrawable = (GradientDrawable) layerDrawable.getDrawable(0);
GradientDrawable secondDrawable = (GradientDrawable) layerDrawable.getDrawable(1);
firstDrawable.setColor(Color.parseColor("#00000000"));
secondDrawable.setColor(Color.parseColor("#00000000"));
//再设置样式
if (!TextUtils.isEmpty(borderColor) && borderThickness > 0) {
//乘以2是因为部分手机显示不出来太细的线
int thickness = (int) (borderThickness * ratio * 2);
layerDrawable.setLayerInset(1, thickness, thickness, thickness, thickness);
firstDrawable.setColor(Color.parseColor(borderColor));
}
if (!TextUtils.isEmpty(textBackgroundColor)) {
secondDrawable.setColor(Color.parseColor(textBackgroundColor));
}
} catch (Exception e) {
//不管什么错误,都只给背景色,不考虑边框了
setBackgroundColor(Color.parseColor(textBackgroundColor));
}
}
|