「Efficient Android Threading 笔记」- C1 Android Components and the Need for Multiprocessing Android RecyclerView Android Socket Programming Supporting Multiple Screens Make a Reusable UI in Android App Development 如何在 Android Studio 中包含 *.so library,并使用库中定义的方法? 使用 SpannableString 格式化字符串,实现前景色、下划线、超链接、图文混排等 如何使用 bound service 完成进程间通信? 创建自定义视图 Creating custom views 通过 Android Theme & Style 定制应用的样式 「译」Android ViewPropertyAnimator 介绍 Android Animation Interpolator - Android 动画插值器源码笔记 「译」Android Animation in Honeycomb by Chet Haase(Android 3.0系统中的动画机制) 从 Android Sample ApiDemos 中学习 android.animation API 的用法 如何学习 Android Animation? 如何实现 Android ListView「上拉加载更多」? 「译」向Big Nerd Ranch提问:为什么Fragment在Android App开发中非常重要? 分类整理我在 SegmentFault 上针对某些问题作的回答 Android Servcie 后台服务总结笔记 如何在Android设备旋转时暂存数据以保护当前的交互状态? Android Message Handler 消息处理机制总结笔记 如何获取FragmentTabHost中指定标签页的Fragment? Fragment子类必须包含一个public无参构造器 如何更新及替换ViewPager中的Fragment? 如何使用Android UI Fragment开发“列表-详情”界面? 一个Android音频文本同步的英文有声读物App的开发过程 「Android编程权威指南笔记」Android应用本地化 通过jfeinstein10/SlidingMenu实现Android侧滑菜单 为Ubuntu14.04部署Android App的Eclipse开发环境 「Android编程权威指南笔记」使用ListFragment显示列表 「Android编程权威指南笔记」SDK版本与兼容 「Android编程权威指南笔记」Android布局和组件 「Android编程权威指南笔记」UI Fragment 「Android编程权威指南笔记」Activity 第一次开发iOS App和Android的对比总结笔记 「App Training笔记」创建第一个应用 「App Training笔记」开发入门训练大纲 Android APP - 从远程FTP服务器下载文件到本地

Make a Reusable UI in Android App Development

2016年08月13日

It’s important to an App developer to build a reusable, flexible and easy-to-modify UI when develop an Android App. You might imagine that you are developing an App and most of its activities have almost the same layout, like this:

  • The top toolbar bar with a back button, a title view and a close button.
  • The main part of the screen which shows a list view, or a detial view, or a camera preview.
  • The bottom toolbar with two buttons which perform certain operations when clicked.

Using UI fragments separates the UI of your app into building blocks, and create an abstract activity for hosting a fragment.

Copyright: Make a Reusable UI in Android App Development is written by Weiyi.Li http://li2.me

Step1: Create A Generic Fragment-Hosting Layout

  • To efficiently re-use complete layouts, you can use the <include/> tag to embed another layout inside the current layout.
  • To make a spot for the fragment’s view in the activity’s view hierarchy, you can use FrameLayout as a container view for hosting fragment;
  • To lazily inflate layout resources at runtime, you can use ViewStub.

Code Snippet:

toolbar_actionbar.xml code click here.

Step2: Create An Abstract Activity class

Nearly every activity you will create in this app will require the same code:

  • Set the ActionBar title, callbacks when ActionBar’s Back & Close button clicked.
  • Create a fragment instance and commit it.

To avoid typing it again and again, you are going to stash it in an abstract class BasicActivity.java, code snippet::

To inflate bottom toolbar layout for activity which has a bottom toolbar, create another abstract class BasicOperationActivity.java which extends BasicActivity.java, code snippet:

Step3: Create A Single Fragment Activtiy

Then you will create a single fragment activity, code snippet:

Step4: Create Two Fragments Activity

If the activity has more than one fragment, you will use show & hide methods to manager these fragments, code snippet:

The End.


知识共享许可协议
li2的博客WeiYi.Li 创作,采用 知识共享 署名-非商业性使用 4.0 国际 许可协议进行许可。
© 2011-2022. All rights reserved by WeiYi.Li. Powerd by Jekyll & LinAnYa's Theme