package com.xuan.forens.actionhttp;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by forens.xuan on 2017/7/9 14:37
* e-mail: 1129305620@qq.com
* des:
*/
public class CommonTitleBar extends RelativeLayout {
private ImageView imageViewBack;
private TextView textViewTitle;
private Context mContext;
public CommonTitleBar(Context context) {
super(context);
}
public CommonTitleBar(Context context, AttributeSet attrs) {
super(context, attrs);
initCommonTitleBar(context,attrs);
}
public CommonTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initCommonTitleBar(context,attrs);
}
private void initCommonTitleBar(Context context,AttributeSet attrs){
this.mContext=context;
LayoutInflater.from(context).inflate(R.layout.layout_title_bar, this);
imageViewBack= (ImageView) findViewById(R.id.img_common_bar_back);
textViewTitle= (TextView) findViewById(R.id.tv_common_bar_title);
/**
* title赋值
*/
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CommonTitleBar);
String text=a.getString(R.styleable.CommonTitleBar_TitleBarTitle);
if(text!=null)
textViewTitle.setText(text);
/**
* 返回键是否显示
*/
boolean show=a.getBoolean(R.styleable.CommonTitleBar_TitleBarShowBack,true);
imageViewBack.setVisibility(show?VISIBLE:GONE);
/**
* 背景设置
*/
setBackgroundColor(a.getColor(R.styleable.CommonTitleBar_TitleBarColor, Color.WHITE));
/**
* 返回键监听
*/
imageViewBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
try {
Activity activity= (Activity) mContext;
activity.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xuan.forens.actionhttp.MainActivity">
<com.xuan.forens.actionhttp.CommonTitleBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:TitleBarTitle="首页"
app:TitleBarColor="@color/colorPrimaryDark">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:text="保存" />
</com.xuan.forens.actionhttp.CommonTitleBar>
<com.xuan.forens.actionhttp.MsgTextView
android:id="@+id/msg_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@android:drawable/ic_menu_call"
android:text="电话拨号" />
</LinearLayout>
属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CommonTitleBar">
<attr name="TitleBarTitle" format="string"/>
<attr name="TitleBarShowBack" format="boolean"/>
<attr name="TitleBarColor" format="color"/>
</declare-styleable>
</resources>