[原创]自定义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自带的错误结果信息: ...
随机推荐
- PHP字符处理基础知识
<?php class StrDemo { function StrTest() { $s = "abcd"; print '$s length:'.strlen($s).& ...
- 在Ubuntu下爽快开发Android必要的5款装备
每一个程序员都有一颗极客的心,一些小装备肯定就比不可少啦.我刚刚从windows中转到Ubuntu,除了要适应ubuntu外,也想将windows中用惯了的小软件一起搬过去.在这里简单地罗列一下自己在 ...
- git tag查看、创建与删除
tag管理 查看tag 太多的话用 grep 过滤 $ git tag | grep dev_20150525 # 创建tag $ git tag -a dev_20150525_16 -m 'xxx ...
- Python变量/运算符/函数/模块/string
Python笔记(一) 1.变量类型 Python 有五个内置的简单类型:bool.int.long.float 和 complex.这些类型是不可变的,就是说整数对象一旦创建,其类型便不可更改. t ...
- [ucgui] 彩色条函数
/* 颜色条 */ void ShowColorBar(void) { , y0 = , yStep = , i; int NumColors = LCD_GetDevCap(LCD_DEVCAP_N ...
- JS动态修改页面EasyUI datebox不生效、EasyUI动态添加Class、EasyUI动态渲染解析解决方案
这是个小菜在实际工作中遇到的问题,相信很多EasyUI新手很可能也遇到这样的问题,因此小菜觉得有必要拿出来分享一下. 这个问题要从EasyUI的datebox组件说起,小菜用这个组件的时候,发现用$( ...
- Jquery对文本框的值、字符串的验证;正则表达式字符串的验证
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Leetcode 342 Power of Four 数论
题意:判断一个数是不是4的幂数,和Power of two类似. 先判断num是否大于0,再判断num是否能开根号,最后判断num开根号后的数是否是2^15的约数. 提示:4的幂数开根号就是2的幂数. ...
- Extjs4中RadioGroup的赋值与取值
1.定义rg var rg = new Ext.form.RadioGroup({ fieldLabel : "test", items : [{ boxLabel : '每天', ...
- Arduino单片机使用和开发问题记录
1.将程序上传到板子时Arduino IDE提示“avrdude: stk500_getsync(): not in sync: resp=0x00” 网上查遇到这个问题的人比较多,有说驱动问题的,有 ...