安卓学习笔记---自定义app顶部标题栏
2016-12-08 12:26
465 查看
几乎每一个安卓应用在顶部都有一个标题栏,有返回,标题,编辑等其他显示的控件,这些控件在每一个页面都会显示,那么又是怎么实现的呢?
最笨的方法就是每一个页面都去重新写一遍,就有了大量重复的代码,也浪费了大量的时间,如果还有点击事件,那无疑在代码中又写了很多重复的代码,蓝瘦香菇有木有
比较好的方式就是我们只写一个文件,然后让其他的布局文件来引用即可,简单高效。
下面看看如何实现:借鉴郭神的思路
引入布局:
首先我们创建一个title_bar.xml文件,用来定义标题栏需要展示哪些控件,代码如下所示:
定义了两个按钮,一个显示标题的文本。
这样我们就可以在每个xml文件中来引入了,代码如下:
这里调用了getSupportActionBar()方法来获得ActivityBar的实例,然后在调用ActionBar的hide()方法将标题栏隐藏起来。再次运行程序,效果如下:
创建自定义控件
引入布局的技巧确实解决了重复编写布局代码的问题,但是如果布局中有一些控件要求能够响应事件,我们还是需要在每个活动中为这些控件单独编写一次事件注册代码。比如说标题栏中的返回按钮,其实不管是在哪个活动中,这个按钮功能都是相同的,即销毁当前活动。而如果在每一个活动中都需要重新注册一遍返回按钮的点击事件,无疑会增加很多重复代码,这种情况最好是使用自定义控件的方式解决。
代码如下:
给出了按钮的点击事件以及显示活动标题的方法,接下来如何使用呢?
首先来看xml文件:
添加自定义控件和添加普通控件的方式基本是一样的,只不过在添加自定义控件的时候,我们需要指明控件的完整类名,包名在这里是不可以省略的。
接下来我们就看在代码中如何使用:
运行效果如下:
看到了吗,使用就是这么简单,赶快用上吧,减少很多代码呢。
最笨的方法就是每一个页面都去重新写一遍,就有了大量重复的代码,也浪费了大量的时间,如果还有点击事件,那无疑在代码中又写了很多重复的代码,蓝瘦香菇有木有
比较好的方式就是我们只写一个文件,然后让其他的布局文件来引用即可,简单高效。
下面看看如何实现:借鉴郭神的思路
引入布局:
首先我们创建一个title_bar.xml文件,用来定义标题栏需要展示哪些控件,代码如下所示:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp" android:background="#00ffff" > <Button android:id="@+id/btnBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" android:layout_alignParentLeft="true" /> <TextView android:id="@+id/tv_title" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:text="标题" android:layout_centerInParent="true" /> <Button android:id="@+id/btnEdit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="编辑" android:layout_alignParentRight="true" /> </LinearLayout>
定义了两个按钮,一个显示标题的文本。
这样我们就可以在每个xml文件中来引入了,代码如下:
<LinearLayout 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" android:orientation="vertical"> <include layout="@layout/title_bar"></include> </LinearLayout>
运行之后,效果如下所示:
同时我们还看到了安卓自带了标题栏功能,这里我们使用代码先隐藏掉,代码如下所示:
android.support.v7.app.ActionBar actionBar=getSupportActionBar(); if(actionBar!=null) { actionBar.hide(); }
这里调用了getSupportActionBar()方法来获得ActivityBar的实例,然后在调用ActionBar的hide()方法将标题栏隐藏起来。再次运行程序,效果如下:
创建自定义控件
引入布局的技巧确实解决了重复编写布局代码的问题,但是如果布局中有一些控件要求能够响应事件,我们还是需要在每个活动中为这些控件单独编写一次事件注册代码。比如说标题栏中的返回按钮,其实不管是在哪个活动中,这个按钮功能都是相同的,即销毁当前活动。而如果在每一个活动中都需要重新注册一遍返回按钮的点击事件,无疑会增加很多重复代码,这种情况最好是使用自定义控件的方式解决。
代码如下:
/** * 自定义标题栏 */ public class TitleLayout extends LinearLayout{ private TextView tv_title; public TitleLayout(Context context) { super(context,null); } public TitleLayout(final Context context, AttributeSet attrs) { super(context, attrs); //引入布局 LayoutInflater.from(context).inflate(R.layout.title_bar,this); Button btnBack=(Button)findViewById(R.id.btnBack); Button btnEdit=(Button)findViewById(R.id.btnEdit); btnBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { ((Activity)getContext()).finish(); } }); btnEdit.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context,"编辑",Toast.LENGTH_SHORT).show(); } }); tv_title=(TextView)findViewById(R.id.tv_title); } //显示活的的标题 public void setTitle(String title) { if(!TextUtils.isEmpty(title)) { tv_title.setText(title); } } }
给出了按钮的点击事件以及显示活动标题的方法,接下来如何使用呢?
首先来看xml文件:
<LinearLayout 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" android:orientation="vertical" android:background="#ff00ff" tools:context="com.test.helloworld.MainActivity"> <com.test.helloworld.view.TitleLayout android:id="@+id/titlelayout" android:layout_width="match_parent" android:layout_height="wrap_content"> </com.test.helloworld.view.TitleLayout>
</LinearLayout>
添加自定义控件和添加普通控件的方式基本是一样的,只不过在添加自定义控件的时候,我们需要指明控件的完整类名,包名在这里是不可以省略的。
接下来我们就看在代码中如何使用:
public class SeconeActivity extends BaseActivity {
private TitleLayout titlelayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secone);
//隐藏系统的标题栏
android.support.v7.app.ActionBar actionBar=getSupportActionBar(); if(actionBar!=null) { actionBar.hide(); }
titlelayout=(TitleLayout)findViewById(R.id.titlelayout);
//显示活动自定义标题
titlelayout.setTitle("第二个页面");
}
}
运行效果如下:
看到了吗,使用就是这么简单,赶快用上吧,减少很多代码呢。
相关文章推荐
- 【iOS学习笔记】通过自定义的URL Scheme启动你的App
- 安卓学习笔记之自定义Toast
- 安卓自定义控价学习笔记(一)
- 安卓学习笔记--- Android自定义View(CustomCalendar-定制日历控件)
- 学习笔记:只有一套app设计稿(5s尺寸)切出4和4s尺寸以及安卓系统主流尺寸的图
- 安卓学习笔记--- Toast拓展--自定义显示时间和动画
- 安卓学习笔记---自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示(矩形圆角加边框)
- 安卓学习笔记---fragment不断切换app崩溃的解决办法
- 安卓学习笔记之自定义组合控件
- 安卓自定义View学习笔记-目录
- 安卓Studio学习笔记---gradle自定义BuildConfig.DEBUG实现在调试输出Log,正式的时候不输出Log
- 安卓学习笔记之AlertDialogUtils实现快速创建自定义AlertDialog
- 安卓学习笔记---自定义漂亮的Android SeekBar样式
- 新手笔记之安卓自定义title标题栏使用
- 安卓学习笔记---android RecyclerView首字母悬浮在顶部,滑动删除
- 安卓智慧上海学习笔记系列——自定义圆环进度条
- 安卓学习笔记---使用浏览器打开app指定协议(Url Scheme)
- 安卓学习笔记---仿今日头条最强顶部导航指示器,支持6种模式
- 安卓学习随笔 -- 自定义标题栏
- 安卓学习笔记---完美解决Android的WebView加载失败(404,500),显示的自定义视图