[原创]自定义BaseAcitivity的实现,统一activity的UI风格样式

    在开发过程中经常遇到多个activity是同一种样式类型的情况,如果分别对其进行UI的布局,不但比较繁琐,而且后续维护过程人力成本很高,不利于敏捷开发。解决的方案是采用抽象后的BaseActivity。
 
    BaseActivity一般作为一个app的所有或者部分activity的父类,并覆写setContentView( )等方法,以达到继承此BaseActivity的多个界面采用较为统一的样式进行开发,同时我们可以扩展BaseActivity的接口,提供一些灵活的个性化方式。下面是实现的主要步骤。
 
1.首先,为想要统一的样式做一个layout资源框架,用来提供UI显示的内容,一般来说可以使用上部ToolBar+下部ViewGroup的方式,并更改theme。
 
BaseActivity的layout资源:
 
<?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 >
2.编写BaseActivity,为了使继承于BaseActivity的activity可以使用通常的函数进行UI控制,在BaseActivity中覆写setContentView( )方法。
下面是BaseActivity的实现:
 
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;
    }
   
}
4.上述代码中用到的布局文件:
activity_main.xml
 
<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>
原文地址:https://www.cnblogs.com/carbs/p/5436336.html