[原创]自定义BaseAcitivity的实现,统一activity的UI风格样式
<?xml version= "1.0" encoding ="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" > <RelativeLayout
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#44550000" /> <RelativeLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#44005500" /> </LinearLayout>
自定义的theme,去掉ActionBar:
<style name="AppThemeNoActionBar" parent="AppBaseTheme" >
<item name= "android:windowActionBar" >false </item>
<item name= "android:windowNoTitle" >true </item>
</style >
package cn.carbs.testandroidbaseactivity; public class BaseActivity extends Activity{ protected RelativeLayout content;
protected int colorPrimary ;
protected int colorPrimaryDark ;
protected int colorAccent ; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(getBaseActivityLayout());
TypedArray array = obtainStyledAttributes( new int[]{R.attr.colorPrimary, R.attr. colorPrimaryDark, R.attr.colorAccent});
colorPrimary = array.getColor(0, 0xFF1473AF);
colorPrimaryDark = array.getColor(1, 0xFF11659A);
colorAccent = array.getColor(2, 0xFF3C69CE);
array.recycle();
} protected int getBaseActivityLayout() {
return R.layout.activity_base;
} @Override
public void setContentView(int layoutResID) {
//使用如下方法可以将layoutResID对应的 xml资源的view解析出来,并添加到R.id.content中
// getLayoutInflater().inflate(layoutResID, (ViewGroup) this.findViewById(R.id.content));
//使用如下方法可以将layoutResID对应的 xml资源的view解析出来,并添加到R.id.content中
// View.inflate(this, layoutResID, (ViewGroup) this.findViewById(R.id.content));
//使用如下方法可以将layoutResID对应的 xml资源的viewinflate出来,但是没有添加到任何ViewGroup中
// View v = View.inflate(this, layoutResID, null); setContentView(View. inflate(this, layoutResID, null));
} @Override
public void setContentView(View view) {
((ViewGroup) this.findViewById(R.id.content))
.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams. MATCH_PARENT));
} }
3.编写一个Activity使其继承自BaseActivity:
package cn.carbs.testandroidbaseactivity; import android.os.Bundle; public class MainActivity extends BaseActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
} //可以通过如下代码,覆写BaseActivity中的主界面的布局
@Override
protected int getBaseActivityLayout(){
return R.layout. activity_base_new;
} }
<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"
android:background= "#33333399"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop= "@dimen/activity_vertical_margin"
tools:context= ".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>
activity_base.xml
<?xml version= "1.0" encoding ="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" > <RelativeLayout
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#44550000" /> <RelativeLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#44005500" /> </LinearLayout>
activity_base_new.xml
<?xml version= "1.0" encoding ="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" > <RelativeLayout
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ffffffff" /> <RelativeLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#44005500" /> </LinearLayout>
[原创]自定义BaseAcitivity的实现,统一activity的UI风格样式的更多相关文章
- 统一项目中编码风格(Eclipse Java code format、codetemplate)
在公司内的日常开发过程中,除了需要遵守统一的编码规范之外,还需要对编写的代码做统一的格式化,Eclipse提供了格式化编码的工具,快捷键是:Ctrl+Shift+F. 为了统一项目组的代码风格,建议使 ...
- Activity中UI框架基本概念
Activity中UI框架基本概念 Activity 是应用程序的基本组成部分,提供了可视的界面,UI容器, 与用户进行交互: 具体Acitivity是怎么样显示这些事视图元素以及响应事件交互的. 一 ...
- android开发之在activity中控制另一个activity的UI更新
转自:http://blog.csdn.net/jason0539/article/details/18075293 第一种方法: 遇到一个问题,需要在一个activity中控制另一个acitivit ...
- 用 Prettier 统一团队的代码风格~
使用 prettier 自動調整 JavaScript 樣式 GFM 格式说明 为什么你不能缺少Linter(以及代码美化工具) 使用 prettier 自動調整 JavaScript 樣式 Reac ...
- Android 如何利用Activity的Dialog风格完成弹出框设计
在我们使用Dialog时,如果需要用到很多自己设计的控件,虽然可以让弹出框显示出我们需要的界面,但却无法找到地方完成控制代码的编写,如何解决这个问题呢,我们可以将Activity伪装成Dialog弹出 ...
- Activity + 基础UI
目录 Activity + 基础UI 1. 返回键退出进入应用: 2. home键以后 3. home键退出切换字体 二:AndroidManifest配置theme改变UI效果 三:UI 报错: A ...
- 自定义的IntentFileter 无法找到activity
<intent-filter > <action android:name="com.leo.enjoytime.VIEW"/></intent-fi ...
- android自定义view系列:认识activity结构
标签: android 自定义view activity 开发中虽然我们调用Activity的setContentView(R.layout.activity_main)方法显示View视图,但是vi ...
- 配置springboot在访问404时自定义返回结果以及统一异常处理
在搭建项目框架的时候用的是springboot,想统一处理异常,但是发现404的错误总是捕捉不到,总是返回的是springBoot自带的错误结果信息. 如下是springBoot自带的错误结果信息: ...
随机推荐
- 从原生APK反编译,拿到界面,用于mono for android
从原生APK反编译,拿到界面,用于mono for android 1.用apktool反编译apk,得到xxx.apk.de 2.从xxx.apk.de\res\layout 3.复制所有xml到M ...
- codeforces 425D
题意:给定n<=100000个二维点,并且0<=x,y<=100000,求有多少个平行于坐标轴的正方形 思路:本来想hash的,但是感觉不好弄.. 后来感觉像是分块,最坏的情况就是那 ...
- 从DB灌值到DataTable时,字段值为NULL时报错相关信息;
报错信息: 1. 2. 3. 4. 5. 6. 解决方法: 1. Data Layer SQL 语句取数据时,把其列值有为null的字段用0.00替换,(ISNULL的用法): 2. #r ...
- elixir 高可用系列(三) GenEvent
概述 GenEvent 是事件处理的通用部分的抽象. 通过 GenEvent ,我们给已有的服务 动态 的添加 事件处理. GenEevent 和 GenServer 的区别 之前已经介绍了 GenS ...
- 各种有用的PHP开源库精心收集
转自:http://my.oschina.net/caroltc/blog/324024 摘要 各种有用的PHP开源库精心收集,包含图片处理,pdf生成,网络协议,网络请求,全文索引,高性能搜索,爬虫 ...
- RabbitMQ(一) -- Hello world
RabbitMQ -- Hello world RabbitMQ使用AMQP通信协议(AMQP是一个提供统一消息服务的应用层标准协议,基于此协议的客户端与消息中间件可传递消息,并不受客户端/中间件不同 ...
- Cocoa编程开发者手册
Cocoa编程开发者手册(Objective-C权威著作超一流翻译阵容) [美] 奇斯纳尔(Chisnall,D.) 著 霍炬等 译 ISBN 978-7-121-12239-2 2013年7月出版 ...
- ubuntu上安装mysql 编译安装
为什么要折腾?首先说明的是ubuntu上安装mysql等软件是非常容易简单的,其简单的程度盖过windows上的安装,一句sudo apt-get install就可以搞定.如果想用最简便的方法安装m ...
- Symbols of String Pattern Matching
Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...
- hdu 1548 楼梯 bfs或最短路 dijkstra
http://acm.hdu.edu.cn/showproblem.php?pid=1548 Online Judge Online Exercise Online Teaching Online C ...