Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SuperTextView 完美支持 #373

Open
zippo88888888 opened this issue Jun 18, 2020 · 0 comments
Open

SuperTextView 完美支持 #373

zippo88888888 opened this issue Jun 18, 2020 · 0 comments

Comments

@zippo88888888
Copy link

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" />

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

@zippo88888888 zippo88888888 changed the title SupterTextView 完美支持 SuperTextView 完美支持 Jun 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant