開發(fā)中可能會遇到要求文字根據(jù)固定寬度兩端對齊的需求拥诡,比如登錄的用戶名標簽和密碼標簽,但TextView沒有自帶這樣的功能冗懦,需要自定義view來實現(xiàn)
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計算文字長度實現(xiàn)兩端對齊效果
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="房間號"/>
<!--其他代碼省略了-->