CommonTitleBar

基本使用
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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,967评论 19 139
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,170评论 6 13
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,959评论 6 342
  • 1、窗体 1、常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程序中可通过Name属性来引用窗体。 ...
    Moment__格调阅读 4,596评论 0 11
  • 四月,没有二三月的冷,也没有五六月的热。四月,在中间,冷暖适宜,很适合在外面晒太阳。 四月,应该是桃红柳绿、草长莺...
    遺釋悟塵阅读 431评论 0 1