示例效果图:
如何实现?
1.创建一个layout,重新设置标题栏的格局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/leftButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp" />
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:gravity="center" />
<Button
android:id="@+id/rightButton"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp" />
</RelativeLayout>
2.编辑自定义属性文件,用于设置标题栏内容
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Topbar">
<attr name="leftBackground" format="reference" />
<attr name="rightBackground" format="reference" />
<attr name="titleText" format="string" />
<attr name="titleTextSize" format="dimension" />
<attr name="titleTextColor" format="color" />
</declare-styleable>
</resources>
3.编写Topbar的控件,继承RelativeLayout,获取自定义属性并给对应的控件赋值 。
package com.example.cactus.topbardemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* 自定义Topbar
*/
public class Topbar extends RelativeLayout {
private Button leftButton, rightButton;
private TextView titleTextView;
private OnLeftAndRightClickListener listener;//监听点击事件
//设置监听器
public void setOnLeftAndRightClickListener(OnLeftAndRightClickListener listener) {
this.listener = listener;
}
//按钮点击接口
public interface OnLeftAndRightClickListener {
void OnLeftButtonClick();
void OnRightButtonClick();
}
//设置左边按钮的可见性
public void setLeftButtonVisibility(boolean flag){
if (flag)
leftButton.setVisibility(View.VISIBLE);
else
leftButton.setVisibility(View.GONE);
}
//设置右边按钮的可见性
public void setRightButtonVisibility(boolean flag){
if (flag)
rightButton.setVisibility(View.VISIBLE);
else
rightButton.setVisibility(View.GONE);
}
public Topbar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.layout_topbar, this);
leftButton = (Button) findViewById(R.id.leftButton);
rightButton = (Button) findViewById(R.id.rightButton);
titleTextView = (TextView) findViewById(R.id.titleText);
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.OnLeftButtonClick();//点击回调
}
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.OnRightButtonClick();//点击回调
}
}
});
//获得自定义属性并赋值
TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.Topbar);
int leftBtnBackground = typeArray.getResourceId(R.styleable.Topbar_leftBackground, 0);
int rightBtnBackground = typeArray.getResourceId(R.styleable.Topbar_rightBackground, 0);
String titleText = typeArray.getString(R.styleable.Topbar_titleText);
float titleTextSize = typeArray.getDimension(R.styleable.Topbar_titleTextSize, 0);
int titleTextColor = typeArray.getColor(R.styleable.Topbar_titleTextColor, 0x38ad5a);
//释放资源
typeArray.recycle();
leftButton.setBackgroundResource(leftBtnBackground);
rightButton.setBackgroundResource(rightBtnBackground);
titleTextView.setText(titleText);
titleTextView.setTextSize(titleTextSize);
titleTextView.setTextColor(titleTextColor);
}
}
4.设置标题栏左右按钮点击变色的效果
左按钮设置:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/b1" android:state_pressed="true"/>
<item android:drawable="@drawable/b2" />
</selector>
右按钮设置:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/r1" android:state_pressed="true"/>
<item android:drawable="@drawable/r2" />
</selector>
5.在需要的xml布局中添加 Topbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.cactus.topbardemo.MainActivity">
<com.example.cactus.topbardemo.Topbar
android:id="@+id/topbar"
android:layout_width="368dp"
android:layout_height="50dp"
android:background="#000"
app:leftBackground="@drawable/leftselecter"
app:rightBackground="@drawable/rightselecter"
app:titleText="我的首页"
app:titleTextColor="#FFF"
app:titleTextSize="6sp"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
6.在对应的Activity中定义点击后实现的功能
package com.example.cactus.topbardemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Topbar topBar = (Topbar) findViewById(R.id.topbar);
topBar.setOnLeftAndRightClickListener(new Topbar.OnLeftAndRightClickListener() {
@Override
public void OnLeftButtonClick() {
// 左边按钮实现的功能逻辑
Toast.makeText(MainActivity.this, "哈哈", Toast.LENGTH_SHORT).show();
}
@Override
public void OnRightButtonClick() {
// 右边按钮实现的功能逻辑
Toast.makeText(MainActivity.this, "RightButton", Toast.LENGTH_SHORT).show();
}
});
}
}
结束了吗? 并没有,这时候你运行的结果将会是这样:
所以你需要去掉原有的标题栏,即做如下操作:
在Mainfesst.xml中修改主题
android:theme="@style/Theme.AppCompat.NoActionBar"
内容参考来源:链接:https://www.jianshu.com/p/0763114e7738