第一种:include
1.设置一个需要的xml布局
2.activity_main.xml中引用这个布局
<include layout="@layout/titlebat"/>
3.AndroidManifest.xml中
将:android:theme="@style/APPTheme">改成
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
第二种方法:
1.先在values下新建一个attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--标题中自定义设置的东西-->
<declare-styleable name="CustomTitleBar">
<attr name="left_btn_drawable" format="reference|integer"/>
<attr name="right_btn_drawable" format="reference|integer"/>
<attr name="middle_text_title" format="string"/>
<attr name="title_background_color" format="reference|color"/>
</declare-styleable>
</resources>
2.在layout中设置布局,titlebar.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/title_bar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@null"
android:minHeight="45dp"
android:minWidth="45dp"
android:textSize="14sp" />
<TextView
android:id="@+id/title_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:textSize="17sp"/>
<Button
android:id="@+id/title_bar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="7dp"
android:background="@null"
android:minHeight="45dp"
android:minWidth="45dp"
android:textSize="14sp" />
</RelativeLayout>
3.创建一个CustomTitleBar.class
package com.example.ruan.dingbubiaotilan;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
/*
*
* 创建一个空间
*
*/
public class CustomTitleBar extends RelativeLayout {
private Button leftButton;
private Button rightButton;
private TextView barTitle;
/**
*
* @param context 上下文对象,所在页面
* @param attrs 属性集合,在布局文件中引用CustomTitleBar设置的属性集合即 android:APP:传到这里来
*/
public CustomTitleBar(Context context, AttributeSet attrs) {
super(context, attrs);
//思路:通过titlebar布局文件找到控件,拿到属性值,把属性值放到控件中
//1.找空间
View view= LayoutInflater.from(getContext()).inflate(R.layout.titlebar,this,true);
leftButton = findViewById(R.id.title_bar_left);
rightButton = findViewById(R.id.title_bar_right);
barTitle = findViewById(R.id.title_bar_title);
//2.拿属性值
TypedArray attributes = context.obtainStyledAttributes(attrs,R.styleable.CustomTitleBar);
//2.1 添加背景颜色
~~**//R.styleable.CustomTitleBar_title_background_color是attrs.xml的name属性**~~
if (attributes!=null){
//拿到背景颜色
int background = attributes.getResourceId(R.styleable.CustomTitleBar_title_background_color, Color.RED);
//设置到背景上
setBackgroundResource(background);
//设置左边图片
int leftbtn=attributes.getResourceId(R.styleable.CustomTitleBar_left_btn_drawable,R.mipmap.ic_launcher);
//设置到titleBarLeftBtn控件的左侧
leftButton.setCompoundDrawablesWithIntrinsicBounds(leftbtn,0,0,0);
//设置右边图片
int rightbtn=attributes.getResourceId(R.styleable.CustomTitleBar_right_btn_drawable,-1);
//设置到titleBarRightBtn控件的右侧
rightButton.setCompoundDrawablesWithIntrinsicBounds(0,0,rightbtn,0);
//设置中间标题文字
String middletext=attributes.getString(R.styleable.CustomTitleBar_middle_text_title);
if (!TextUtils.isEmpty(middletext)){
barTitle.setText(middletext);
}
}
attributes.recycle();//回收typedArray对象,用于后续调用时复用
}
}