Skip to content

SuperTextView 完美支持 #373

Open
@zippo88888888

Description

@zippo88888888
import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.coorchice.library.SuperTextView
import skin.support.app.SkinLayoutInflater
import skin.support.content.res.SkinCompatResources
import skin.support.widget.SkinCompatHelper
import skin.support.widget.SkinCompatSupportable
import skin.support.widget.SkinCompatTextHelper

class SkinSuperTextView : SuperTextView, SkinCompatSupportable {

    private var mTextHelper: SkinCompatTextHelper? = null
    private var mStrokeColorResId = SkinCompatHelper.INVALID_ID
    private var mSolidColorResId = SkinCompatHelper.INVALID_ID

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        mTextHelper = SkinCompatTextHelper(this)
        mTextHelper?.loadFromAttributes(attrs, defStyleAttr)

        val array = context.obtainStyledAttributes(attrs, R.styleable.SuperTextView, defStyleAttr, 0)

        mStrokeColorResId = array.getResourceId(
            R.styleable.SuperTextView_stv_stroke_color,
            SkinCompatHelper.INVALID_ID
        )

        mSolidColorResId = array.getResourceId(
            R.styleable.SuperTextView_stv_solid,
            SkinCompatHelper.INVALID_ID
        )

        array.recycle()
        applySolidColor()
        applyStrokeColor()
    }

    private fun applyStrokeColor() {
        mStrokeColorResId = SkinCompatHelper.checkResourceId(mStrokeColorResId)
        if (mStrokeColorResId != SkinCompatHelper.INVALID_ID) {
            val color = SkinCompatResources.getColor(context, mStrokeColorResId)
            super.setStrokeColor(color)
        }

    }

    private fun applySolidColor() {
        mSolidColorResId = SkinCompatHelper.checkResourceId(mSolidColorResId)
        if (mSolidColorResId != SkinCompatHelper.INVALID_ID) {
            val color = SkinCompatResources.getColor(context, mSolidColorResId)
            super.setSolid(color)
        }
    }

    override fun setTextAppearance(resId: Int) {
        super.setTextAppearance(resId)
        mTextHelper?.onSetTextAppearance(context, resId)
    }

    override fun setTextAppearance(context: Context?, resId: Int) {
        super.setTextAppearance(context, resId)
        mTextHelper?.onSetTextAppearance(context, resId)
    }

    override fun setSolid(solid: Int): SuperTextView {
        mSolidColorResId = solid
        applySkin()
        return this
    }

    override fun setStrokeColor(strokeColor: Int): SuperTextView {
        mStrokeColorResId = strokeColor
        applySkin()
        return this
    }

    override fun applySkin() {
        applyStrokeColor()
        applySolidColor()
        mTextHelper?.applySkin()
    }


    class SkinSuperTextViewInflater : SkinLayoutInflater {

        override fun createView(context: Context, name: String?, attrs: AttributeSet): View? {
            var view: View? = null
            when (name) {
                "com.coorchice.library.SuperTextView" -> {
                    view = SkinSuperTextView(context, attrs)
                }
            }
            return view
        }

    }

}
<com.coorchice.library.SuperTextView
            android:id="@+id/item_task_alarmWBTxt"
            android:layout_width="70dp"
            android:layout_height="25dp"
            android:layout_marginStart="@dimen/default_divider_padding"
            android:layout_marginEnd="7dp"
            android:gravity="center"
            android:text="@string/app_task_wb_str"
            android:textColor="@color/baseColor"
            android:textSize="11sp"
            app:stv_corner="13dp"
            app:stv_solid="@color/item_task_hj_solid_btn2_color1"
            app:stv_stroke_color="@color/item_task_hj_stroke_btn2_color1"
            app:stv_stroke_width="0.3dp"
            tools:ignore="SmallSp" />

有需要的复制即可直接食用!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions