一、概述
这片博客主要是介绍通过自定义view的方式,实现一个在应用中经常使用的导航设计:Topbar,可以将其自定义为一个控件,通过xml文件的方式设置其各种属性,简化代码,提高工作效率。
如下图所示,是新浪微博的几个界面的截图,看红色方框内,每个界面都有这么一个Topbar导航栏设计。如果使用android系统给我们提供的控件,每一个界面都需要实现在RelativeLayout,左边加leftButton 中间TitleView 右边rightButton。如果我们将其设计成topbar,当需要改变topbar布局主题什么的只需要简单就该布局文件即可。
二、初探
在android中引用一个控件如下所示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.topbar.MainActivity"
RelativeLayout />
android中定义一个控件思路:在atts中定义好各种需要设置的属性类型等,再定义一个控件继承View或其某个子类,然后再子类中将定义的各种需求。
三 自定义topbar
1.在values目录下新建atts文件设置各种需要的属性。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<attr name="titleText" format = "string"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="titleTextColor" format="color"/>
<attr name="leftTextColor" format="color"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="leftText" format = "string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
<attr name="rightText" format = "string"/>
</declare-styleable>
</resources>
2.新建TopBar类实现各种功能。
(1).给Topbar添加三个控件leftButton 、tvTitle、rightButton
并设置每个控件的各种属性:背景色,文字内容、文字大小等。
public class TopBar extends RelativeLayout{
private Button leftButton, rightButton;
private TextView tvTitle;
private int leftTextColor;
private Drawable leftBackground;
private String leftTitle;
private int rightTextColor;
private Drawable rightBackground;
private String rightTitle;
private float titleTextSize;
private int titleTextColor;
private String titleText;
private LayoutParams leftParams, rightParams, titleParams;
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
//这个变量可以从attrs文件中读取各种属性。
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TopBar);
//读取ta中的各种属性数据
leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);
leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);
leftTitle = ta.getString(R.styleable.TopBar_leftText);
rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0);
rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);
rightTitle = ta.getString(R.styleable.TopBar_rightText);
titleText = ta.getString(R.styleable.TopBar_titleText);
titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);
titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 0);
ta.recycle();
//初始化三个控件
leftButton = new Button(context);
rightButton = new Button(context);
tvTitle = new TextView(context);
//设置左边Button的各种属性
leftButton.setTextColor(leftTextColor);
leftButton.setBackground(leftBackground);
leftButton.setText(leftTitle);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightBackground);
rightButton.setText(rightTitle);
tvTitle.setText(titleText);
tvTitle.setTextColor(titleTextColor);
tvTitle.setTextSize(titleTextSize);
tvTitle.setGravity(Gravity.CENTER);
setBackgroundColor(0xFF101010);
//添加三个控件到topBar中
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftButton,leftParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightButton, rightParams);
titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(tvTitle,titleParams);
// 设置leftButton显示或者是隐藏。
public void setLeftIsvisable(boolean flag){
if(flag){
leftButton.setVisibility(View.VISIBLE);
}else{
leftButton.setVisibility(View.GONE);
}
}
}
(2)为左右button设置监听事件。
设置监听事件需要使用到java回调函数,如果你不熟悉请点击连接https://www.zhihu.com/question/19801131。
private TopBarClickListener topBarClickListener;
//在Topbar中定义内部接口
public interface TopBarClickListener{
public void leftClick();
public void rightClick();
}
//提供设置监听事件方法。
public void setOnTopBarClickListener(TopBarClickListener listener){
this.topBarClickListener = listener;
}
//左button设置监听事件
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
topBarClickListener.leftClick();
}
});
//有button设置监听事件
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
topBarClickListener.rightClick();
}
});
(3)在程序中引用自定义的Topbar
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.topbar.MainActivity"
xmlns:app="http://schemas.android.com/apk/res/com.example.topbar">
<com.example.topbar.TopBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/top_bar"
app:leftTextColor="#FFFFFFFF"
app:leftBackground="@drawable/button"
app:leftText="Back"
app:rightTextColor="#FFFFFFFF"
app:rightBackground="@drawable/button"
app:rightText="Menu"
app:titleText="Title"
app:titleTextSize="9dp"
app:titleTextColor="#FFFFFFFF"
></com.example.topbar.TopBar>
</RelativeLayout>
最后运行结果如下图所示
三、总结
本博客所涉及的代码项目源码地址在这里
https://github.com/dronly/MyTopBar
这边博客是在某课网上学习了eclipse_xu大神的课程后,自己梳理的流程,再次谢过xu大神。
这里是课程地址http://www.imooc.com/learn/247
这是我的第一篇博客诶,文字难免会有不当之处,甚至有些语句不顺。希望读者可以理解。