自定义TitleBar

隐藏android自带的标题栏,实现自定义的简单标题栏.

package com.xspacing.register.view;

import com.xspacing.register.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @ClassName CustomTitleBar.java
 * @Description 自定义标题栏
 * @author Smile
 * @version v1.0
 * @date 2016年9月20日 上午9:15:03
 */
public class CustomTitleBar extends LinearLayout {

    private Context mContext;
    private onClickListener listener;
    private int back_imgeview;
    private String title_text;
    private int text_color;
    private float title_size;

    public CustomTitleBar(Context context) {
        super(context);
        mContext = context;
        initCustomTitleBar();
    }

    public CustomTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        //获取自定义attrs属性
        TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.CustomTitleBar);
        back_imgeview = ta.getResourceId(R.styleable.CustomTitleBar_back_imgeview, -1);
        title_text = ta.getString(R.styleable.CustomTitleBar_title_text);
        text_color = ta.getColor(R.styleable.CustomTitleBar_title_text_color, -1);
        title_size = ta.getDimension(R.styleable.CustomTitleBar_title_size, -1);
        ta.recycle();
        initCustomTitleBar();
    }

    public CustomTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initCustomTitleBar();
    }
    //接口
    public interface onClickListener {
        public void onClick(View v);
    }

    private void initCustomTitleBar() {
        this.setOrientation(LinearLayout.HORIZONTAL);
        this.setGravity(Gravity.CENTER_VERTICAL);
        LayoutParams mLayoutParams1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ImageView mImageView = new ImageView(mContext);
        mImageView.setImageResource(back_imgeview);
        addView(mImageView, mLayoutParams1);
        LayoutParams mLayoutParams2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
        TextView mTextView = new TextView(mContext);
        mTextView.setText(title_text);
        mTextView.setTextColor(text_color);
        mTextView.setTextSize(title_size);
        mTextView.setGravity(Gravity.CENTER);
        addView(mTextView, mLayoutParams2);
        mImageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //传参数给接口
                listener.onClick(v);
            }
        });
    }
    //接口回调方法
    public void setOnClickListener(onClickListener listener) {
        this.listener = listener;
    }
}

value文件下的attrs自定义属性文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomTitleBar">
        <attr name="back_imgeview" format="reference"></attr>
        <attr name="title_text" format="string"></attr>
        <attr name="title_text_color" format="color"></attr>
        <attr name="title_size" format="dimension"></attr>
    </declare-styleable>

</resources>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2F65C3"
    tools:context="com.xspacing.register.MainActivity" >

    <com.xspacing.register.view.CustomTitleBar
        android:id="@+id/main_customtitlebar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        app:back_imgeview="@drawable/back"
        app:title_size="8sp"
        app:title_text="快速注册"
        app:title_text_color="#FFFFFF" >
    </com.xspacing.register.view.CustomTitleBar>
    </RelativeLayou>

实现返回图标的监听

CustomTitleBar mCustomTitleBar = (CustomTitleBar) findViewById(R.id.main_customtitlebar);

mCustomTitleBar.setOnClickListener(new onClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "返回", 0).show();
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值