Android富文本实现

作者: 友人A_fc9c | 来源:发表于2018-07-09 17:22 被阅读2285次

QRichText

效果图

效果图1 效果图2

添加依赖

  1. 在项目根目录的build.gradle中添加如下语句
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
  1. 在module下的build.gradle中添加如下语句
    dependencies {
            implementation 'com.github.Gennki:QRichText:v1.0.3'
    }

使用方法

1. xml布局如下:

<cn.qzb.richeditor.RichEditor
        android:id="@+id/editor"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

2. 在Activity的onCreate中初始化RichEditor

// init rich text editor
val re = RE.getInstance(editor)
re.setPlaceHolder("Input text here...")
re.setPadding(20, 20, 20, 20)
re.setTextBackgroundColor(Color.WHITE)

3. 常用api

Api 说明
re.getHtml() 获取富文本html代码
re.setBold() 加粗的时候调用这个方法为取消加粗,没有加粗的时候调用这个方法为加粗
re.setItalic() 斜体,使用方法和加粗相同
re.setUnderLine() 下划线,使用方法和加粗相同
re.setTextSize(fontSize) 字号大小,fontSize范围为1~7的整数
re.insertImage(url, alt, imageWidthPercent) url为图片路径,alt为图片下方显示的说明文字,暂时不支持,imageWidthPercent为图片宽度占一屏幕的百分比,默认为100
re.setTextColor(color) 设置字体颜色
re.setTextBackgroundColor(color) 设置字体背景颜色
re.moveToEndEdit() 使用场景一般为要编辑某段富文本的时候,刚进入页面的时候,光标要显示到最后,并且编辑框的内容也要滑动到底部, 需要注意的是,刚进入页面的时候马上调用此方法可能会无效,因为页面还没有渲染好 最好延时几百毫秒后调用

相关文章

网友评论

  • a07e5a5d3bc8:老铁,怎么添加显示手机本地图片呀?麻烦说一下。谢谢啦。
    友人A_fc9c:@a07e5a5d3bc8 :sunglasses:
    a07e5a5d3bc8:@友人A_fc9c 好的。谢谢。可以了。
    友人A_fc9c:把url路径换成你的本地路径就可以啦,例如SD卡下面有一张test.jpg,那么url就是
    Environment.getExternalStorageDirectory().path+"/test.jpg"
    千万要注意要给读写权限哦
  • 周紫一:小程序怎么实现富文本,大佬
    友人A_fc9c:小程序是纯web前端的吧,实现起来应该比较简单,可以参考下百度的UEditor

本文标题:Android富文本实现

本文链接:https://www.haomeiwen.com/subject/gngzuftx.html