最近开发遇到一个下面这种布局结构,关系到Textview的换行,并且第二行与前端图片对齐,后段追加时间的样式。
![image.png](https://upload-images.jianshu.io/upload_images/9625409-e3b9c20ffa0c2b36.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

我们都知道在Android中所有的控件都是矩形的,所以想通过单个Textview实现评论内容换行,并且与前段对齐市无法实现的,那么我们换个思路如果我们能够知道评论内容第一行最多能显示几个字符,那么剩下的就可以通过将内容拆分成两个Textview来实现,而关于后面追加的时间样式可以SpannableStringBuilder对第二个Textview进行修饰。

首先我们来看看怎么获取第一行最多显示多少字符。

xml布局
```  
        android:id="@+id/v_text"
        android:layout_width="100dp"
        android:maxLines="1"
        android:text="dsadsadsasfreqrwqewqewqewqewqewq"
        android:layout_height="wrap_content"/> 
```
这里我们限制了宽度和最大行数,内容必然会被截断。
kotlin
 ```
v_text.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                v_text.viewTreeObserver.removeOnGlobalLayoutListener(this)
                val layout = v_text.layout
                val lineEnd = layout.getLineEnd(0)
            }
        })
```
 layout.getLineEnd(0)返回的就是第一行最后一个字符的位置。

思路有了,那么我们就 使用组合自定义控件方式来实现,以便复用:

布局部分:
```
<?xml version="1.0" encoding="utf-8"?>
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


            android:id="@+id/v_hot_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="13dp"
        android:layout_marginTop="3dp"
        android:layout_marginRight="7dp"
        android:background="@color/colorPrimary"
        android:drawablePadding="2dp"
        android:gravity="center_vertical"
        android:paddingLeft="3dp"
        android:paddingRight="3dp"
        android:text="热评"
        android:textColor="#ff333333"
        android:textSize="11sp"
        android:textStyle="bold" />

            android:id="@+id/v_hot_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_icon3" />

            android:id="@+id/v_content_line1"
        android:maxLines="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/v_hot_text" />

            android:id="@+id/v_content_line2"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/v_content_line1" />

```

kotlin部分
```
package com.lucas.test

import android.content.Context
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView

/**
 * @package    CommentChildView.kt
 * @author     luan
 * @date       2019-11-06
 * @des        评论内容区
 */
class CommentChildView : FrameLayout {
    constructor(context: Context) : super(context) {
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(context)
    }

    var rootView: ViewGroup? = null
    var contentStr: String = ""

    fun initView(context: Context) {
        rootView = LayoutInflater.from(context).inflate(
            R.layout.view_comment_child,
            null,
            false
        ) as ViewGroup
        addView(rootView)
    }

    fun setContent(content: String) {
        contentStr = content
        rootView?.apply {
            val line1 = findViewById(R.id.v_content_line1)
            line1.text = content
            //获取剩余未显示的字符串
            val lineEnd = line1.layout.getLineEnd(0)
            if (content.length == lineEnd) {//第一行已显示完,无需使用第二行显示

            } else {//启用第二行显示剩余字符
                val substring = content.substring(lineEnd, content.length)
                findViewById(R.id.v_content_line2).text = substring
            }
        }
    }

    fun isHot(isHot: Boolean) {
        rootView?.apply {
            if (isHot) {
                findViewById(R.id.v_hot_icon).visibility = View.VISIBLE
                findViewById(R.id.v_hot_text).visibility = View.VISIBLE
            } else {
                findViewById(R.id.v_hot_icon).visibility = View.GONE
                findViewById(R.id.v_hot_text).visibility = View.GONE
            }
        }
    }

    //追加时间样式--调用该方法前需先调用setContent,否则追加时间位置会出错
    fun setTime(time: String) {
        rootView?.apply {
            val line1 = findViewById(R.id.v_content_line1)
            val line2 = findViewById(R.id.v_content_line2)
            //判断追加第几行
            if (line2.text.toString().isEmpty()) {//追加第一行
                line1.text = "${line1.text}  ${time}"
                //如果追加后超出一行,则放弃,改为追加到二行
                if (isSingleLine(line1)) {
                    line1.text = contentStr
                    initTimeStyle(line2, time)
                }else{
                    line1.text = contentStr
                    initTimeStyle(line1, time)
                }
            } else {//追加第二行
                line1.text = contentStr
                initTimeStyle(line2, time)
            }
        }
    }

    private fun initTimeStyle(line2: TextView, time: String) {
        line2.text = "${line2.text} $time"
        //修改时间样式
        val line2Str = line2.text
        val timeIndex = line2Str.indexOf(time)
        val builder = SpannableStringBuilder(line2Str)
        builder.setSpan(
            ForegroundColorSpan(Color.parseColor("#999999")),
            timeIndex,
            timeIndex + time.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        builder.setSpan(
            AbsoluteSizeSpan(11, true),
            timeIndex,
            timeIndex + time.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        line2.text = builder
    }

    //判断textview是否超出一行
    private fun isSingleLine(textView: TextView): Boolean {
        val lineEnd = textView.layout.getLineEnd(0)
        return textView.text.length > lineEnd
    }
}
```

具体运行效果:
![image.png](https://upload-images.jianshu.io/upload_images/9625409-c34d0db076b91b5d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

其中有点需要注意的是,在Textview没有绘制完成前textview.getLayout方法返回的是空的,使用时需要在合适的时机使用例如:
```
<?xml version="1.0" encoding="utf-8"?>
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    

            android:id="@+id/v_comment"
        android:layout_width="200dp"
        android:layout_height="wrap_content"/>


```

```
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

       v_comment.viewTreeObserver.addOnGlobalLayoutListener(object :ViewTreeObserver.OnGlobalLayoutListener{
           override fun onGlobalLayout() {
               v_comment.viewTreeObserver.removeOnGlobalLayoutListener(this)
               v_comment.setContent("法规和冯绍峰dasdadasdauguyguihiuh")
               v_comment.isHot(true)
               v_comment.setTime("12-11")
           }
       })
    }
}

```


 

更多相关文章

  1. Android(安卓)中状态栏(屏幕顶部)消息的显示 Notification
  2. 谈谈Android中的SurfaceTexture
  3. Android百度地图开发(五)公交线路详情搜索、多条线路显示
  4. 图片裁剪问题
  5. 2012-06-13 16:50 Android限定EditText的输入类型为数字或者英文
  6. android log 日志分析 来自 Google I/O 2011: Memory man...
  7. activity介绍和值的传递(寒假学习2)
  8. ScrollView嵌套ViewPager,ViewPager内容不显示问题
  9. android之Notification通知

随机推荐

  1. 初学XML的基础知识-认识XML的作用
  2. 解析XML和JSON内容的一点技巧的实例代码
  3. 详细介绍json数据格式和xml数据格式的区
  4. XML关于图像超链接的制作的代码实例
  5. JSON和XML-不可同日而语的详解
  6. XML和Tomcat的入门知识的详细介绍
  7. Microsoft的XMLHTTP对象详解
  8. 不是Web开发者所关注的XML基础具体分析
  9. 关于XML文档的基本操作的实例代码分享
  10. Parse XML Tree 解析XML文件的代码实例