Android 自定義Toolbar

封裝下基本功能:返回鍵,title,功能鍵等。

CustomToolbar

class CustomToolbar @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : RelativeLayout(context, attributeSet, defStyleAttr, defStyleRes) {
    private val view = LayoutInflater.from(context).inflate(R.layout.toolbar, this, true)
    private var back: ImageView
    private var title: TextView
    private var feature: TextView
    private var icon: ImageView
    private var split: View
    private var backListener: Callback? = null
    private var featureListener: Callback? = null
    private var iconListener: Callback? = null

    init {
        back = view.findViewById(R.id.toolbarBack)
        title = view.findViewById(R.id.toolbarContent)
        feature = view.findViewById(R.id.toolbarFeature)
        icon = view.findViewById(R.id.toolbarIcon)
        split = view.findViewById(R.id.toolbarSplit)

        val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CustomToolbar)
        val content = typedArray.getString(R.styleable.CustomToolbar_barContent)
        val contentColor = typedArray.getColor(
            R.styleable.CustomToolbar_barContentColor,
            context.resources.getColor(R.color.textBlack)
        )
        val featureText = typedArray.getString(R.styleable.CustomToolbar_featureText)
        val featureTextColor = typedArray.getColor(
            R.styleable.CustomToolbar_featureTextColor,
            context.resources.getColor(R.color.blue_266EFF)
        )
        val iconVisible = typedArray.getBoolean(R.styleable.CustomToolbar_barIcon, false)
        val splitVisible = typedArray.getBoolean(R.styleable.CustomToolbar_barSplit, true)

        typedArray.recycle()

        title.text = content
        title.setTextColor(contentColor)
        feature.text = featureText
        feature.setTextColor(featureTextColor)
        feature.visibility = if (featureText.isNullOrEmpty()) View.GONE else View.VISIBLE
        icon.visibility = if (iconVisible) View.VISIBLE else View.GONE
        split.visibility = if (splitVisible) View.VISIBLE else View.GONE

        back.setOnClickListener {
            backListener?.invoke()
        }
        feature.setOnClickListener {
            featureListener?.invoke()
        }
        icon.setOnClickListener {
            iconListener?.invoke()
        }
    }

    fun backListener(callback: Callback): CustomToolbar {
        backListener = callback
        return this
    }

    fun featureListener(callback: Callback): CustomToolbar {
        featureListener = callback
        return this
    }

    fun iconListener(callback: Callback): CustomToolbar {
        iconListener = callback
        return this
    }

    fun setContent(content: String): CustomToolbar {
        title.text = content
        return this
    }

    fun setContentColor(color: Int): CustomToolbar {
        title.setTextColor(color)
        return this
    }

    fun setFeatureText(text: String): CustomToolbar {
        feature.text = text
        feature.visibility = if (text.isEmpty()) View.GONE else View.VISIBLE
        return this
    }

    fun setFeatureTextColor(color: Int): CustomToolbar {
        feature.setTextColor(color)
        return this
    }

    fun setIconVisible(boolean: Boolean): CustomToolbar {
        icon.visibility = if (boolean) View.VISIBLE else View.GONE
        return this
    }

    fun setSplitVisible(boolean: Boolean): CustomToolbar {
        split.visibility = if (boolean) View.VISIBLE else View.GONE
        return this
    }
}

Alias

internal typealias Callback = () -> Unit

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <ImageView
        android:id="@+id/toolbarBack"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_centerVertical="true"
        android:layout_marginStart="16dp"
        android:contentDescription="@null"
        android:src="@drawable/toolbar_back" />

    <TextView
        android:id="@+id/toolbarContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:textColor="@color/textBlack"
        android:textSize="20sp"
        tools:ignore="RelativeOverlap" />

    <TextView
        android:id="@+id/toolbarFeature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="16dp"
        android:gravity="center"
        android:textColor="@color/blue_266EFF"
        android:textSize="18sp"
        tools:ignore="RelativeOverlap" />

    <ImageView
        android:id="@+id/toolbarIcon"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="16dp"
        android:contentDescription="@null"
        android:src="@drawable/toolbar_menu" />

    <View
        android:id="@+id/toolbarSplit"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_alignParentBottom="true"
        android:background="@color/split" />
</RelativeLayout>

自定義屬性attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomToolbar">
        <attr name="barContent" format="string" />
        <attr name="barContentColor" format="color" />
        <attr name="featureText" format="string" />
        <attr name="featureTextColor" format="color" />
        <attr name="barIcon" format="boolean" />
        <attr name="barSplit" format="boolean" />
    </declare-styleable>
</resources>

使用

    <com.chenxuan.widget.CustomToolbar
        android:id="@+id/loginToolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:barContent="登錄"
        app:barIcon="true"
        app:barSplit="false"
        app:featureText="更多"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Activity

toolbar.run {
    setContent("title")
    setFeatureText("更多")
    setIconVisible(true)
    setSplitVisible(true)
    backListener {
        finish()
    }
    featureListener {

    }
    iconListener {

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章