【已解决】 TextView.setGravity(Gravity.CENTER)没居中
自定义view里面代码生成textview,设置了setGravity,不知道为什么文字偏下了
====================关键代码================================================
textView = TextView(context)
textView.setTextColor(this.mNumColor)
textView.background = provideItemBg()
textView.setOnClickListener { }
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, this.mNumberSize.toFloat())
if (i == 10) {
textView.tag = true
} else {
textView.tag = false
textView.text =i.toString()
}
textView.setGravity(Gravity.CENTER)
addView(textView)
===================完整代码=================================================
class NumKeyboardLayout(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
ViewGroup(context, attrs, defStyleAttr) {
companion object {
const val DEFAULT_NUMBER_SIZE = 20
const val DEFAULT_RADIU = 0
}
//大小单位px
private var mNumberSize = 0
//圆角单位px
private var mRadiu = 0
@ColorInt
private var mNumColor: Int = -1
@ColorInt
private var mItemBg: Int = -1
@ColorInt
private var mItemPressBg: Int = -1
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context) : this(context, null)
init {
initAttrs(context, attrs)
initSetUpItem()
}
private fun initSetUpItem() {
removeAllViews()
var textView: TextView
for (i in 0..10) {
textView = TextView(context)
textView.setTextColor(this.mNumColor)
textView.background = provideItemBg()
textView.setOnClickListener { }
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, this.mNumberSize.toFloat())
if (i == 10) {
textView.tag = true
} else {
textView.tag = false
textView.text =i.toString()
}
textView.setGravity(Gravity.CENTER)
addView(textView)
}
}
private fun provideItemBg(): StateListDrawable {
//背景图按下 shape
val itemBgPressDrawable = GradientDrawable()
itemBgPressDrawable.setColor(this.mItemPressBg)
itemBgPressDrawable.cornerRadius = this.mRadiu.toFloat()
//背景图正常 shape
val itemBgNormalDrawable = GradientDrawable()
itemBgNormalDrawable.setColor(this.mItemBg)
itemBgNormalDrawable.cornerRadius = this.mRadiu.toFloat()
//背景selector
val itemBgSelector = StateListDrawable()
itemBgSelector.addState(intArrayOf(android.R.attr.state_pressed), itemBgPressDrawable)
itemBgSelector.addState(intArrayOf(), itemBgNormalDrawable)
return itemBgSelector
}
private fun initAttrs(context: Context, attrs: AttributeSet?) {
val attrs = context.obtainStyledAttributes(attrs, R.styleable.NumKeyboardLayout)
this.mNumberSize = attrs.getDimensionPixelOffset(
R.styleable.NumKeyboardLayout_nkl_numberSize,
ConvertUtils.dp2px(DEFAULT_NUMBER_SIZE.toFloat())
)
LogUtils.d("mNumberSize ==>$this.mNumberSize")
this.mNumColor = attrs.getColor(
R.styleable.NumKeyboardLayout_nkl_numberColor,
context.resources.getColor(R.color.white)
)
this.mItemBg = attrs.getColor(
R.styleable.NumKeyboardLayout_nkl_itemBg,
context.resources.getColor(R.color.darkGray)
)
this.mItemPressBg = attrs.getColor(
R.styleable.NumKeyboardLayout_nkl_itemPressBg,
context.resources.getColor(R.color.darkGrayDark)
)
this.mRadiu = attrs.getDimensionPixelOffset(
R.styleable.NumKeyboardLayout_nkl_radiu,
DEFAULT_RADIU
)
attrs.recycle()
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val widthMode = MeasureSpec.getMode(widthMeasureSpec)
val widthSize = MeasureSpec.getSize(widthMeasureSpec)
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
val heightSize = MeasureSpec.getSize(heightMeasureSpec)
LogUtils.d("widthSize==> $widthSize")
LogUtils.d("heightSize==> $heightSize")
//item宽度
val itemWidth = widthSize / 3
//item高度
val itemHeight = heightSize / 4
val normalWidthSpace = MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY)
val heightSpace = MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY)
val deleteWidthSpace = MeasureSpec.makeMeasureSpec(itemWidth * 2, MeasureSpec.EXACTLY)
//测量孩子
for (i in 0 until childCount) {
val view = getChildAt(i)
view.measure(if (view.tag == true) deleteWidthSpace else normalWidthSpace, heightSpace)
}
//测量自己
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec)
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
val item = getChildAt(0)
item.layout(0, 0, item.measuredWidth, item.measuredWidth)
}
override fun getResources(): Resources {
return super.getResources()
}
/**
* 设置数字大小,单位px
* @param size Int
*/
fun setNumberSize(size: Int) {
this.mNumberSize = size
}
fun setNumColor(@ColorInt color: Int) {
this.mNumColor = color
}
fun setItemBg(@ColorInt color: Int) {
this.mItemBg = color
}
fun setItemPressBg(@ColorInt color: Int) {
this.mItemPressBg = color
}
}

不好说,有可能是java代码的问题。也有可能是xml文件的问题。试一下这个方法:
我运行了一下,是没得问题的,我是直接用RelativeLayout包裹的。
我今天比较忙,出去了。我指个方向吧。解决这种问题,可以看布局。布局看不出来问题可以通过LayoutInpsector查看一下,在tools里头。