零散的知识点

这篇博客主要介绍了如何在Android中实现TextView内容可滚动并显示滚动条,通过使用不同的构造方法和风格设置来解决在不同Android版本上的兼容问题。同时,展示了如何通过Java代码解析XML文件来设置layer_list类型的Drawable背景,并动态调整其颜色和边框样式。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值