您的位置:首页 > 移动开发 > Android开发

结合Toolbar自定义状态栏

2016-06-20 00:00 411 查看
首先看图:



你没有看错,这是结合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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息