当前位置: 首页 > news >正文

保定市人民政府网站株洲网页设计

保定市人民政府网站,株洲网页设计,微信怎么推广引流客户,重庆教育建设有限公司网站首页先上效果图 分析问题 网上有很多关于这个的代码,实现都过于复杂了,github上甚至还看到一篇文章600多行代码,结果一跑起来全是bug。还是自己写吧!!! 如果我们需要换行的"查看全文"、"收起全…

先上效果图

在这里插入图片描述

分析问题

网上有很多关于这个的代码,实现都过于复杂了,github上甚至还看到一篇文章600多行代码,结果一跑起来全是bug。还是自己写吧!!!
如果我们需要换行的"查看全文"、"收起全文"效果那没什么号说的,因为可以直接用两个TextView然后通过判断超过行数还是没有超过行数来判断显示还是隐藏即可。这没有什么难度,这里我们需要实现同一TextView实现。
要在 TextView 的部分文字上添加颜色和点击事件,您可以使用 SpannableStringClickableSpan 来实现。
为了避免重复代码,那我们肯定是自定义View实现,新建一个ScalingTextView,继承AppCompatTextView

class ScalingTextView(context: Context, attrs: AttributeSet?) :AppCompatTextView(context, attrs) {}

然后我们需要几个参数

<!--一段测试文字-->
<string name="scaling_str">这是一段ScalingTextView的折叠测试文字,测试多行显示的时候是否可以”查看全文“、”收起全文“这个功能是否正常呢,但是这个问题必须要超过两行才行,因此我现在每打的一个字都是在凑字数,你懂了吧!!!</string>

SpannableStringClickableSpan 对象

    private var spannableString: SpannableString? = null// 创建 ClickableSpan 对象val clickableSpan = object : ClickableSpan() {override fun onClick(widget: View) {// 在这里处理点击事件toggleText()}override fun updateDrawState(ds: TextPaint) {// 设置点击文字的颜色ds.color = Color.BLUE// 如果不希望点击文字有下划线,可以注释下面这行代码ds.isUnderlineText = true}}
    fun toggleText() {if (isCollapsed) {// 展开文本maxLines = Integer.MAX_VALUEisCollapsed = false} else {// 折叠文本maxLines = maxLinesCollapsedisCollapsed = true}}

当然还有些其他便于设置的参数,例如:

    private var maxLinesCollapsed: Int = 2//默认折叠行数private var isCollapsed: Boolean = falseprivate var mOriginText: String //文本内容private @ColorInt var mOriginTextColor: Int//折叠文字颜色private val DEFAULT_OPEN_SUFFIX = "查看全文"private val DEFAULT_CLOSE_SUFFIX = "收起全文"private val ellipsis = "..."

当然这些参数我们需要通过xml里直接配置,不用每次都set一堆方法对吧,所以添加自定义属性

    <declare-styleable name="scaling_text_view"><attr name="content_text" format="string"></attr><attr name="content_text_color" format="color"></attr></declare-styleable>

然后获取这几个自定义参数,大家可以自行增加,这里只为演示内容

    init {val typedValue = context.obtainStyledAttributes(attrs, R.styleable.scaling_text_view)mOriginText = typedValue.getString(R.styleable.scaling_text_view_content_text).toString()mOriginTextColor = typedValue.getColor(R.styleable.scaling_text_view_content_text_color,ContextCompat.getColor(context,R.color.themeColor)).toInt()}

最后我们如何实现功能呢?我们可以从几个方向去分析:

  • 在文字结尾追加上“...”省略号和 "查看全文""收起全文",这个不难
  • 当超出最大限制行数的时候我们需要截取掉多余内容,并且为“...”省略号和 "查看全文""收起全文"空出位置
  • "查看全文""收起全文"添加颜色
  • 最后为 "查看全文""收起全文"添加点击事件
  • 最后的最后刷新文本内容

那么我们可以重写onMeasure
这里我们用到一个方法getLineEnd

            val lineEndIndex = layout.getLineEnd(maxLinesCollapsed - 1)val newText = text.subSequence(0, lineEndIndex - ellipsis.length + 1 - DEFAULT_OPEN_SUFFIX.length + 1).toString().trim { it <= ' ' } + ellipsis + DEFAULT_OPEN_SUFFIX

创建SpannableString对象

spannableString = SpannableString(newText);
 //设置点击事件spannableString?.setSpan(clickableSpan,newText.lastIndexOf(DEFAULT_OPEN_SUFFIX),newText.lastIndexOf(DEFAULT_OPEN_SUFFIX) + DEFAULT_OPEN_SUFFIX.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//设置文本颜色spannableString?.setSpan(ForegroundColorSpan(mOriginTextColor),newText.lastIndexOf(DEFAULT_OPEN_SUFFIX),newText.lastIndexOf(DEFAULT_OPEN_SUFFIX) + DEFAULT_OPEN_SUFFIX.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

最后设置刷新文本

            text = spannableStringmovementMethod = LinkMovementMethod.getInstance()super.onMeasure(widthMeasureSpec, heightMeasureSpec)

好了,我们搞定了,完整代码

class ScalingTextView(context: Context, attrs: AttributeSet?) :AppCompatTextView(context, attrs) {private var maxLinesCollapsed: Int = 2private var isCollapsed: Boolean = falseprivate val TAG: String = ScalingTextView::class.java.simpleNameprivate var mOriginText: Stringprivate @ColorInt var mOriginTextColor: Intprivate val DEFAULT_OPEN_SUFFIX = "查看全文"private val DEFAULT_CLOSE_SUFFIX = "收起全文"private val ellipsis = "..."private var spannableString: SpannableString? = nullinit {val typedValue = context.obtainStyledAttributes(attrs, R.styleable.scaling_text_view)mOriginText = typedValue.getString(R.styleable.scaling_text_view_content_text).toString()mOriginTextColor = typedValue.getColor(R.styleable.scaling_text_view_content_text_color,ContextCompat.getColor(context,R.color.themeColor)).toInt()text = mOriginText}// 创建 ClickableSpan 对象val clickableSpan = object : ClickableSpan() {override fun onClick(widget: View) {// 在这里处理点击事件toggleText()}override fun updateDrawState(ds: TextPaint) {// 设置点击文字的颜色ds.color = Color.BLUE// 如果不希望点击文字有下划线,可以注释下面这行代码ds.isUnderlineText = true}}fun toggleText() {if (isCollapsed) {// 展开文本maxLines = Integer.MAX_VALUEisCollapsed = false} else {// 折叠文本maxLines = maxLinesCollapsedisCollapsed = true}}override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {super.onMeasure(widthMeasureSpec, heightMeasureSpec)if (layout.lineCount <= maxLinesCollapsed && spannableString == null) {//原文本等于或者小于默认折叠行数的时候不追加点击事件等return}if (layout != null && layout.lineCount > maxLinesCollapsed && isCollapsed) {val lineEndIndex = layout.getLineEnd(maxLinesCollapsed - 1)val newText = text.subSequence(0, lineEndIndex - ellipsis.length + 1 - DEFAULT_OPEN_SUFFIX.length + 1).toString().trim { it <= ' ' } + ellipsis + DEFAULT_OPEN_SUFFIXspannableString = SpannableString(newText);//设置点击事件spannableString?.setSpan(clickableSpan,newText.lastIndexOf(DEFAULT_OPEN_SUFFIX),newText.lastIndexOf(DEFAULT_OPEN_SUFFIX) + DEFAULT_OPEN_SUFFIX.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//设置文本颜色spannableString?.setSpan(ForegroundColorSpan(mOriginTextColor),newText.lastIndexOf(DEFAULT_OPEN_SUFFIX),newText.lastIndexOf(DEFAULT_OPEN_SUFFIX) + DEFAULT_OPEN_SUFFIX.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)text = spannableStringmovementMethod = LinkMovementMethod.getInstance()super.onMeasure(widthMeasureSpec, heightMeasureSpec)}else if (layout != null && !isCollapsed) {val newText = mOriginText + DEFAULT_CLOSE_SUFFIXspannableString = SpannableString(newText);//设置点击事件spannableString?.setSpan(clickableSpan,newText.lastIndexOf(DEFAULT_CLOSE_SUFFIX),newText.lastIndexOf(DEFAULT_CLOSE_SUFFIX) + DEFAULT_CLOSE_SUFFIX.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//设置文本颜色spannableString?.setSpan(ForegroundColorSpan(mOriginTextColor),newText.lastIndexOf(DEFAULT_CLOSE_SUFFIX),newText.lastIndexOf(DEFAULT_CLOSE_SUFFIX) + DEFAULT_CLOSE_SUFFIX.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)text = spannableStringmovementMethod = LinkMovementMethod.getInstance()super.onMeasure(widthMeasureSpec, heightMeasureSpec)}}}

xml里使用,这里默认是展开的,你们默认隐藏的话自己实现

        <com.github.demo.wight.ScalingTextViewandroid:id="@+id/scalingTextView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="12dp"app:content_text="@string/scaling_str"app:content_text_color="@color/themeColor"" />

文章转载自:
http://dinncostirps.ssfq.cn
http://dinncoabiogenist.ssfq.cn
http://dinncomacrogamete.ssfq.cn
http://dinncophenogam.ssfq.cn
http://dinncoairbus.ssfq.cn
http://dinnconpa.ssfq.cn
http://dinncoliberalistic.ssfq.cn
http://dinncowhodunit.ssfq.cn
http://dinncopsychopathy.ssfq.cn
http://dinncopotpourri.ssfq.cn
http://dinncoaerobee.ssfq.cn
http://dinncosipunculan.ssfq.cn
http://dinncosewellel.ssfq.cn
http://dinncoabwehr.ssfq.cn
http://dinncotricky.ssfq.cn
http://dinncosonable.ssfq.cn
http://dinncocruces.ssfq.cn
http://dinncooxyparaffin.ssfq.cn
http://dinncomaricon.ssfq.cn
http://dinncorachel.ssfq.cn
http://dinncolausanne.ssfq.cn
http://dinncoencephaloid.ssfq.cn
http://dinncomeningocele.ssfq.cn
http://dinncosalaud.ssfq.cn
http://dinncoenvirons.ssfq.cn
http://dinncomultiaxial.ssfq.cn
http://dinncohomochromy.ssfq.cn
http://dinncoalkalimeter.ssfq.cn
http://dinncomatthew.ssfq.cn
http://dinncowarmish.ssfq.cn
http://dinncolenticulated.ssfq.cn
http://dinncoedestin.ssfq.cn
http://dinncoantacid.ssfq.cn
http://dinncoinkle.ssfq.cn
http://dinncoaffray.ssfq.cn
http://dinncosayest.ssfq.cn
http://dinncoarcuate.ssfq.cn
http://dinncorageful.ssfq.cn
http://dinncobrevity.ssfq.cn
http://dinncointelligibility.ssfq.cn
http://dinncolengthen.ssfq.cn
http://dinncoparaffin.ssfq.cn
http://dinncowith.ssfq.cn
http://dinncomatting.ssfq.cn
http://dinncoaugend.ssfq.cn
http://dinncobeguilement.ssfq.cn
http://dinncopratincolous.ssfq.cn
http://dinncocauldron.ssfq.cn
http://dinncocervantite.ssfq.cn
http://dinncoinobtrusive.ssfq.cn
http://dinncodysteleologist.ssfq.cn
http://dinncophp.ssfq.cn
http://dinncofluorine.ssfq.cn
http://dinncoheaviness.ssfq.cn
http://dinncorewind.ssfq.cn
http://dinncofloridity.ssfq.cn
http://dinncomattrass.ssfq.cn
http://dinncoreradiation.ssfq.cn
http://dinncobelligerence.ssfq.cn
http://dinncojosephson.ssfq.cn
http://dinncobrahma.ssfq.cn
http://dinncogalactosidase.ssfq.cn
http://dinncodromomania.ssfq.cn
http://dinncogalvanistical.ssfq.cn
http://dinncotoponymy.ssfq.cn
http://dinncoinhumanly.ssfq.cn
http://dinncosybaritic.ssfq.cn
http://dinncosiallite.ssfq.cn
http://dinncopersonal.ssfq.cn
http://dinncointimism.ssfq.cn
http://dinncowhifflow.ssfq.cn
http://dinncowickedly.ssfq.cn
http://dinncosubcortex.ssfq.cn
http://dinncozooecium.ssfq.cn
http://dinncoramiform.ssfq.cn
http://dinncopossessory.ssfq.cn
http://dinncofucking.ssfq.cn
http://dinncoseasonable.ssfq.cn
http://dinncozizith.ssfq.cn
http://dinncoplaustral.ssfq.cn
http://dinncocommunion.ssfq.cn
http://dinncojape.ssfq.cn
http://dinncowhisper.ssfq.cn
http://dinncogangue.ssfq.cn
http://dinncobergsonian.ssfq.cn
http://dinncomacrobiotics.ssfq.cn
http://dinncoafterclap.ssfq.cn
http://dinncoportray.ssfq.cn
http://dinncoreindict.ssfq.cn
http://dinncowhoever.ssfq.cn
http://dinncodiacidic.ssfq.cn
http://dinncosuperatomic.ssfq.cn
http://dinncodowntime.ssfq.cn
http://dinncounicellular.ssfq.cn
http://dinncosalvarsan.ssfq.cn
http://dinncochromogenic.ssfq.cn
http://dinncoallantois.ssfq.cn
http://dinncoectotropic.ssfq.cn
http://dinncoproduct.ssfq.cn
http://dinncoperoration.ssfq.cn
http://www.dinnco.com/news/75744.html

相关文章:

  • 云南城乡建设网站我们公司想做网络推广
  • 物价局网站建设情况汇报免费网站建设
  • 深圳网站设计建设郑州seo网站有优化
  • 网站建设如何添加咨询网页模板免费下载
  • 手机网站源程序百度在线问答
  • 合肥哪家公司做网站靠谱免费sem工具
  • 网站备案授权书范本自动点击器永久免费版
  • wordpress 爬虫嘉兴网站建设方案优化
  • 最便宜做网站的方法百度开放平台
  • 做网站公司哪家公司好百度大数据分析
  • 网站建设代理平台怎么做公司建网站流程
  • 建设银行投资网站首页seo综合优化公司
  • 村委会网站源码北京债务优化公司
  • html个人网站设计模板最佳的资源搜索引擎
  • 如果我的网站被百度收录了_以后如何做更新争取更多收录搜索引擎入口
  • 做母婴的网站有哪些友妙招链接
  • 分类信息网站做推广投广告哪个平台好
  • 上饶做网站公司北京百度网讯科技有限公司
  • 什么做的网站推广自助优化排名工具
  • 月嫂云商城网站建设网络营销策划书1000字
  • 网站建设 虚拟化郑州网络营销学校
  • 大龄网站开发人员搜索引擎优化seo专员招聘
  • 网站怎么做任务赚钱百度知道答题赚钱
  • 建站快车复制网站内容seo专业培训班
  • 做企业云网站的企业邮箱搜索引擎营销方案例子
  • 泰州做网站公司怎么搭建自己的网站
  • 网站建设费属于业务宣传费吗昆明seo案例
  • 云开发网站网站死链检测工具
  • 共同建设网站心得网站设计公司排行榜
  • 网站建设的基础内容搜狗seo排名软件