android 自定义ToolBar
首先自定义属性
<declare-styleable name="CustomToolBar">
<attr name="back_src" format="reference" />
<attr name="title_text" format="string" />
<attr name="title_text_size" format="dimension" />
<attr name="title_text_color" format="color" />
<attr name="right_title" format="string" />
<attr name="right_title_color" format="color" />
<attr name="right_title_size" format="dimension" />
<attr name="right_src" format="reference" />
<attr name="right_title_visibility" format="enum">
<enum name="gone" value="8" />
<enum name="visible" value="0" />
<enum name="invisible" value="4" />
</attr>
<attr name="line_color" format="color"/>
<attr name="line_height" format="dimension"/>
</declare-styleable>
自定义View集成Linalayout
custom_toolbar布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/rl_back"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_centerVertical="true"
>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_title"
android:layout_centerInParent="true"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/rl_right"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/iv_right"
android:layout_centerInParent="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/tv_right"
/>
</RelativeLayout>
</RelativeLayout>
<View
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="1dp"
/>
</LinearLayout>
package com.example.administrator.livedemo;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* 自定义头布局
*/
public class CustomToolBar extends LinearLayout implements View.OnClickListener {
private Context mContext;
private ImageView mIvBack;
private TextView mTvTitle;
private ImageView mIvRight;
private TextView mTvRight;
private View mLine;
public CustomToolBar(Context context) {
super(context);
this.mContext = context;
initView(context, null, 0);
}
public CustomToolBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
initView(context, attrs, 0);
}
public CustomToolBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
initView(context, attrs, defStyleAttr);
}
private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
LayoutInflater.from(context).inflate(R.layout.custom_toolbar, this);
mIvBack = findViewById(R.id.iv_back);
mTvTitle = findViewById(R.id.tv_title);
mTvRight = findViewById(R.id.tv_right);
mIvRight = findViewById(R.id.iv_right);
mLine = findViewById(R.id.line);
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomToolBar);
int title_text_color = typedArray.getColor(R.styleable.CustomToolBar_title_text_color, 0xff000000);
String title_text = typedArray.getString(R.styleable.CustomToolBar_title_text);
float title_size = typedArray.getDimension(R.styleable.CustomToolBar_title_text_size, 18);
int back_src = typedArray.getResourceId(R.styleable.CustomToolBar_back_src, 0);
int right_src = typedArray.getResourceId(R.styleable.CustomToolBar_right_src, 0);
int right_title_v = typedArray.getInteger(R.styleable.CustomToolBar_right_title_visibility, View.VISIBLE);
String right_title = typedArray.getString(R.styleable.CustomToolBar_right_title);
int right_title_color = typedArray.getColor(R.styleable.CustomToolBar_right_title_color, 0xff000000);
float right_title_size = typedArray.getDimension(R.styleable.CustomToolBar_title_text_size, 16);
float line_height = typedArray.getDimension(R.styleable.CustomToolBar_line_height, 1);
int line_color = typedArray.getColor(R.styleable.CustomToolBar_line_color, 0x00000000);
mIvBack.setImageResource(back_src);
mTvTitle.setText(title_text);
mTvTitle.setTextColor(title_text_color);
mTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, title_size);
mTvRight.setText(right_title);
mTvRight.setTextColor(right_title_color);
mTvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, right_title_size);
mIvRight.setImageResource(right_src);
mTvRight.setVisibility(right_title_v);
mLine.setBackgroundColor(line_color);
ViewGroup.LayoutParams layoutParams = mLine.getLayoutParams();
layoutParams.height = (int) line_height;
mLine.setLayoutParams(layoutParams);
typedArray.recycle();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rl_back:
((Activity) mContext).finish();
break;
case R.id.rl_right:
if (onRightClickListener != null) {
onRightClickListener.onRight();
}
break;
}
}
public interface OnRightClickListener {
void onRight();
}
private OnRightClickListener onRightClickListener;
public void setOnRightClickListener(OnRightClickListener onRightClickListener) {
this.onRightClickListener = onRightClickListener;
}
}
布局文件中使用
<CustomToolBar
android:layout_width="match_parent"
app:back_src="@mipmap/ic_launcher"
app:title_text="Title"
app:title_text_size="18sp"
app:title_text_color="#ff0000"
app:right_src="@mipmap/ic_launcher"
app:right_title="right"
app:right_title_size="16sp"
app:right_title_color="#0000ff"
app:line_height="1dp"
app:line_color="@color/colorAccent"
android:layout_height="wrap_content"></CustomToolBar>
好了 完成 简单的一个ToolBar完成了
很简单 希望能够帮助到大家 第一次写博客写的不好请留言 写下 好的建议!!