android项目框架结构实现、封装、继承

在项目开发时如果app大部分界面的风格类似以及界面转场动画相同,

例如统一样式的titlebar和bottomBar,沉浸样式,中间部分是内容,处理网络异常和没有数据时的显示样式风格相同,那么可以将不变的部分封装到某个activity中,通过继承去现实不同数据



在基础activity中实现沉浸样式

public class BaseActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setupTransparencyStatus();
        ATStatusBarUtils.StatusBarLightMode(this);
    }

    /**
     * 设置透明状态栏
     */
    protected void setupTransparencyStatus() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            final Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(android.R.color.transparent));
        }
    } 
}
public class ATStatusBarUtils {
        /**
         * 修改状态栏为全透明
         * @param activity
         */
        @TargetApi(19)
        public static void transparencyBar(Activity activity){
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);

            } else
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                Window window =activity.getWindow();
                window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
        }

        /**
         * 修改状态栏颜色,支持4.4以上版本
         * @param activity
         * @param colorId
         */
        public static void setStatusBarColor(Activity activity,int colorId) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();
//      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(activity.getResources().getColor(colorId));
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                //使用SystemBarTint库使4.4版本状态栏变色,需要先将状态栏设置为透明
                transparencyBar(activity);
                SystemBarTintManager tintManager = new SystemBarTintManager(activity);
                tintManager.setStatusBarTintEnabled(true);
                tintManager.setStatusBarTintResource(colorId);
            }
        }

        /**
         *设置状态栏黑色字体图标,
         * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
         * @param activity
         * @return 1:MIUUI 2:Flyme 3:android6.0
         */
        public static int StatusBarLightMode(Activity activity){
            int result=0;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                if(MIUISetStatusBarLightMode(activity.getWindow(), true)){
                    result=1;
                }else if(FlymeSetStatusBarLightMode(activity.getWindow(), true)){
                    result=2;
                }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                    result=3;
                }
            }
            return result;
        }

        /**
         * 已知系统类型时,设置状态栏黑色字体图标。
         * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
         * @param activity
         * @param type 1:MIUUI 2:Flyme 3:android6.0
         */
        public static void StatusBarLightMode(Activity activity,int type){
            if(type==1){
                MIUISetStatusBarLightMode(activity.getWindow(), true);
            }else if(type==2){
                FlymeSetStatusBarLightMode(activity.getWindow(), true);
            }else if(type==3){
                activity.getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }

        }

        /**
         * 清除MIUI或flyme或6.0以上版本状态栏黑色字体
         */
        public static void StatusBarDarkMode(Activity activity,int type){
            if(type==1){
                MIUISetStatusBarLightMode(activity.getWindow(), false);
            }else if(type==2){
                FlymeSetStatusBarLightMode(activity.getWindow(), false);
            }else if(type==3){
                activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }

        }


        /**
         * 设置状态栏图标为深色和魅族特定的文字风格
         * 可以用来判断是否为Flyme用户
         * @param window 需要设置的窗口
         * @param dark 是否把状态栏字体及图标颜色设置为深色
         * @return  boolean 成功执行返回true
         *
         */
        public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
            boolean result = false;
            if (window != null) {
                try {
                    WindowManager.LayoutParams lp = window.getAttributes();
                    Field darkFlag = WindowManager.LayoutParams.class
                            .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                    Field meizuFlags = WindowManager.LayoutParams.class
                            .getDeclaredField("meizuFlags");
                    darkFlag.setAccessible(true);
                    meizuFlags.setAccessible(true);
                    int bit = darkFlag.getInt(null);
                    int value = meizuFlags.getInt(lp);
                    if (dark) {
                        value |= bit;
                    } else {
                        value &= ~bit;
                    }
                    meizuFlags.setInt(lp, value);
                    window.setAttributes(lp);
                    result = true;
                } catch (Exception e) {

                }
            }
            return result;
        }

        /**
         * 设置状态栏字体图标为深色,需要MIUIV6以上
         * @param window 需要设置的窗口
         * @param dark 是否把状态栏字体及图标颜色设置为深色
         * @return  boolean 成功执行返回true
         *
         */
        public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
            boolean result = false;
            if (window != null) {
                Class clazz = window.getClass();
                try {
                    int darkModeFlag = 0;
                    Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                    Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                    darkModeFlag = field.getInt(layoutParams);
                    Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                    if(dark){
                        extraFlagField.invoke(window,darkModeFlag,darkModeFlag);//状态栏透明且黑色字体
                    }else{
                        extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                    }
                    result=true;
                }catch (Exception e){

                }
            }
            return result;
        }


}


实现沉浸式后通过继承BaseActivity实现界面动画

public class ATTranslucentActivity extends BaseActivity {
    private ATSwipeBackLayout ATSwipeBackLayout = null;
    private boolean needAnimation = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initSlideBack();
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        initSlideBack();
    }

    protected void onResume() {
        super.onResume();
        ATSwipeBackLayout.dealWithNightMode();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (needAnimation) {
            ATActivityAnimation.leftOut(this);
        }
    }

    public void setNeedAnimation(boolean b) {
        needAnimation = b;
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        if (needAnimation) {
            ATActivityAnimation.rightIn(this);
        }
    }

    /**
     * set sliding mode
     *
     * @param mode
     */
    public void setSlidingMode(ATSwipeBackLayout.Sliding mode) {
        if (ATSwipeBackLayout == null) {
            throw new NullPointerException("ATSwipeBackLayout is null,Please call after the setContentView");
        }
        ATSwipeBackLayout.setSlidingMode(mode);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        if (needAnimation) {
            ATActivityAnimation.rightIn(this);
        }
    }

    @SuppressLint("InflateParams")
    private void initSlideBack() {
        ATSwipeBackLayout = (ATSwipeBackLayout) LayoutInflater.from(this).inflate(R.layout.swipeback_layout, null);
        ATSwipeBackLayout.attachToActivity(this);
    }

    protected void setEnableGesture(boolean b) {
        ATSwipeBackLayout.setEnableGesture(b);
    }
}

再通过继承转场动画activity的实现界面风格统一,例如统一的titleBar和bottomBar和数据为null或者网络异常时的处理

public abstract class ATTitleActivity extends ATTranslucentActivity {

    View mTitleView;
    View failedView;
    View content;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.at_titile_bar);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private void initView() {
       
    }
    //网络加载失败后重新请求网络
    public void clickToRetry(ATErrorPage atErrorPage) {

    }
    public void showNoNetWork() {
        container.setVisibility(View.GONE);
        failedView.setVisibility(View.VISIBLE);
        failedImage.setImageResource(R.drawable.failed_wifi_icon);
        failedFirstText.setText(ATResUtil.getStringById(R.string.at_no_network));
        failedSecondText.setVisibility(View.GONE);
        clickHint.setText(ATResUtil.getStringById(R.string.at_click_wifi_load));
        this.atErrorPage = ATErrorPage.WIFI_ERROR;
    }

    public void showNoData() {
        container.setVisibility(View.GONE);
        failedView.setVisibility(View.VISIBLE);
        failedImage.setImageResource(R.drawable.failed_no_data_icon);
        failedFirstText.setText(ATResUtil.getStringById(R.string.at_no_failed_data));
        failedSecondText.setVisibility(View.GONE);
        clickHint.setText(ATResUtil.getStringById(R.string.at_click_no_data_load));
        this.atErrorPage = ATErrorPage.EMPTY_ERROR;
    }

    public void showServerError(String message) {
        container.setVisibility(View.GONE);
        failedView.setVisibility(View.VISIBLE);
        failedImage.setImageResource(R.drawable.failed_server_icon);
        failedFirstText.setText(ATStringUtil.isEmpty(message) ? (ATResUtil.getStringById(R.string.at_server_error_first_text)) : message);
        failedSecondText.setText(ATResUtil.getStringById(R.string.at_server_error_second_text));
        clickHint.setText(ATResUtil.getStringById(R.string.at_click_wifi_load));
        this.atErrorPage = ATErrorPage.SERVER_ERROR;
    }

    public void showContentView() {
        container.setVisibility(View.VISIBLE);
        failedView.setVisibility(View.GONE);
        this.atErrorPage = ATErrorPage.NONE;
    }
    //子类只在content区域编写界面
    @Override
    public void setContentView(int layoutResID) {
        View view = getLayoutInflater().inflate(layoutResID, null);
       /* if (container == null) {
            finish();
            Intent it = new Intent();
            it.setClass(this, SplashActivity.class);
            it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(it);
            return;
        }*/
        container.addView(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    }

   

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    public void setTitle(CharSequence title) {
        titleText.setText(title);
    }

    public void setTitle(int resouceId) {
        titleText.setText(resouceId);
    }

   

    @SuppressLint("NewApi")
    public void onSetTitleVisible() {
        mTitleView.setVisibility(View.VISIBLE);
    }

    @SuppressLint("NewApi")
    public void onSetTitleGone() {
        mTitleView.setVisibility(View.GONE);
        iv_divider.setVisibility(View.GONE);
    }
    public ViewGroup getTitleView() {
        return mTitleView;
    }

}

到此大体介绍完毕,在沉浸式式默认是没有显示状态栏的,为了显示状态栏需要在
at_titile_bar布局文件中加入
android:fitsSystemWindows="true"即可
 
  
 
  






你可能感兴趣的:(android)