自定义Title

xml:

<jp.co.view.TitleLayout
android:id="@+id/titleLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
TitleLayout:
package jp.co.view

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import jp.co.hyakujushibank.securestarter.R

class TitleLayout(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
private var barImg: ImageView? = null
private var titleTv: TextView? = null
private var closeTv: TextView? = null

init {
LayoutInflater.from(context).inflate(R.layout.layout_title, this)
val activity = (getContext() as Activity)
val backImg = findViewById(R.id.backImg) as ImageView
val closeTv = findViewById(R.id.closeTv) as TextView
val titleTv = findViewById(R.id.titleTv) as TextView
val barImg = findViewById(R.id.barImg) as ImageView
this.barImg = barImg
this.titleTv = titleTv
this.closeTv = closeTv
backImg.setOnClickListener { activity.finish() }
}

fun setBarImageResource(resource: Int) {
barImg!!.setImageResource(resource)
}

fun setTitle(title: String) {
titleTv!!.text = title
}

fun setCloseInvisible(){
closeTv!!.visibility=View.INVISIBLE
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/backImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:src="@mipmap/back_black_btn" />

<jp.co.hyakujushibank.view.JapanTextBoldView
android:id="@+id/titleTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="はじめに"
android:textSize="16dp" />

<jp.co.hyakujushibank.view.JapanTextBoldView
android:id="@+id/closeTv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="中断する"
android:textSize="16dp" />
</LinearLayout>

<View
android:id="@+id/lineView"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_EBEBEB" />

<ImageView
android:id="@+id/barImg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:src="@mipmap/nav_tab1" />
</LinearLayout>
 
 
posted @ 2017-10-13 16:01  改变世界的老十七  阅读(538)  评论(0编辑  收藏  举报