先上效果图开发中可能会遇到要求文字根据固定宽度两端对齐的需求,比如登录的用户名标签和密码标签,但TextView没有自带这样的功能,需要自定义view来实现
Android组件有很多自带的属性,如 android:layout_width、android:layout_height等,使用attrs.xml
文件,可以自己定义属性,这里我在attrs.xml
文件中添加如下属性,用作判断是否需要两端对齐
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AlignTextView">
<attr name="alignOnlyOneLine" format="boolean"/>
</declare-styleable>
</resources>
自定义view计算文字长度实现两端对齐效果
import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.text.StaticLayout
import android.util.AttributeSet
import android.widget.TextView
import com.xzl.electronicclasscard.R
@SuppressLint("AppCompatCustomView")
class AlignTextView : TextView {
private var alignOnlyOneLine = false
constructor(context: Context) : super(context) {
initAttr(context, null)
}
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
initAttr(context, attributeSet)
}
constructor(
context: Context,
attributeSet: AttributeSet,
defStyleAttr: Int
) : super(context, attributeSet, defStyleAttr) {
initAttr(context, attributeSet)
}
private fun initAttr(context: Context, attributeSet: AttributeSet?) {
val typedArray: TypedArray =
context.obtainStyledAttributes(attributeSet, R.styleable.AlignTextView)
alignOnlyOneLine = typedArray.getBoolean(R.styleable.AlignTextView_alignOnlyOneLine, false)
typedArray.recycle()
}
override fun onDraw(canvas: Canvas?) {
if (text !is String) {
super.onDraw(canvas)
} else {
paint.color = currentTextColor
for (i in 0 until layout.lineCount) {
val lineBaseline = layout.getLineBaseline(i) + paddingTop
val lineStart = layout.getLineStart(i)
val lineEnd = layout.getLineEnd(i)
if (alignOnlyOneLine && layout.lineCount == 1) {
val line = text.substring(lineStart, lineEnd)
val width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, paint)
this.drawScaledText(canvas, line, lineBaseline.toFloat(), width)
} else if (i == layout.lineCount - 1) {
canvas?.drawText(
text.substring(lineStart),
paddingLeft.toFloat(),
lineBaseline.toFloat(),
paint
)
break
} else {
val line = text.substring(lineStart, lineEnd)
val width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, paint)
this.drawScaledText(canvas, line, lineBaseline.toFloat(), width)
}
}
}
}
private fun drawScaledText(canvas: Canvas?, line: String, baseLineY: Float, lineWidth: Float) {
if (line.isEmpty()) {
return
}
var x = paddingLeft.toFloat()
val forceNextLine = line[line.length - 1].code == 10
val length = line.length - 1
if (forceNextLine || length == 0) {
canvas?.drawText(line, x, baseLineY, paint)
return
}
val oneTextWidth = (measuredWidth - lineWidth - paddingLeft - paddingRight) / length
for (element in line) {
val textStr = element.toString()
val dw = StaticLayout.getDesiredWidth(textStr, this.paint)
canvas?.drawText(textStr, x, baseLineY, this.paint)
x += dw + oneTextWidth
}
}
}
然后直接在使用布局文件中使用即可
<!--这里使用自己存放该view的包名,我这里是com.test.view-->
<com.test.view.AlignTextView
android:id="@+id/info_room_number"
android:layout_width="170dp"
android:layout_height="wrap_content"
app:alignOnlyOneLine="true"
android:textColor="@color/grey_desc"
android:text="房间号"/>
<!--其他代码省略了-->
网友评论