结合Toolbar自定义状态栏

首先看图:

你没有看错,这是结合toolbar做出来的!

下面说说如何实现:

一、布局

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/titleViewToolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <com.clang.merchant.manage.main.widget.MarqueeTextView
            android:id="@+id/titleViewText"
            style="@style/text_16_ffffff"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:clickable="true"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:singleLine="true"
            tools:text="title" />

    </android.support.v7.widget.Toolbar>
</RelativeLayout>

有人会问为何能使用toolbar去包裹一个view,答案是因为toolbar是继承viewGroup,so!

下面是自定义的一些属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TitleView">
        <attr name="title_text" format="string" />
        <attr name="background_color" format="color" />
    </declare-styleable>
</resources>

二、代码


import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clang.merchant.manage.main.R;

/**
 * @Author: wliang.
 * @Date: 2016/4/25 16:25
 * @E-mail: wl386123298@qq.com
 */
public class TitleView extends RelativeLayout {

    private TextView mTitle;
    private Toolbar mToolBar;

    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);

        String titleText = typedArray.getString(R.styleable.TitleView_title_text);
        int backgroundColor = typedArray.getColor(R.styleable.TitleView_background_color , ContextCompat.getColor(getContext(), R.color.colorPrimary));

        View view = inflate(context, R.layout.title_view_layout, this);

        mToolBar = (Toolbar) view.findViewById(R.id.titleViewToolBar);
        mTitle = (TextView) view.findViewById(R.id.titleViewText);
        mToolBar.setTitle("");

        if (!TextUtils.isEmpty(titleText)){
            mTitle.setText(titleText);
        }

        mToolBar.setBackgroundColor(backgroundColor);

        typedArray.recycle();
    }


    public Toolbar getToolBar() {
        return mToolBar;
    }

    public void setTitle(@Nullable String titleText) {
        mTitle.setText(Html.fromHtml(titleText));
    }


    public void setBackgroundColor(@ColorInt int color){
        mToolBar.setBackgroundColor(color);
    }
}

三、使用方法

  <com.clang.merchant.manage.main.widget.TitleView
        android:id="@+id/orderInfoTitleView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:title_text="订单详情"/>
 TitleView mTitleView = (TitleView) findViewById(R.id.orderInfoTitleView);

 if (mTitleView != null) {
    setSupportActionBar(mTitleView.getToolBar());
 }
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);

 

转载于:https://my.oschina.net/wvliang/blog/694403

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值