版权声明:本文为HaiyuKing原创文章,转载请注明出处!
概述
这里简单记录下Activity、Fragment、Dialog基类中的常规写法,后续根据项目需求进行相应的扩展。
BaseActivity.java
package com.why.project.myapptemplet.activity;import android.content.pm.ActivityInfo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import android.util.Log; import android.view.Window;/*** Created by HaiyuKing* Used*/ public class BaseActivity extends AppCompatActivity {private static final String TAG = BaseActivity.class.getSimpleName();public static int screenWidth;//屏幕宽度public static int screenHeight;//屏幕高度public static int screenDendity;//屏幕密度比例public static int screenDendityDpi;//屏幕密度 @Overrideprotected void onCreate(Bundle savedInstanceState) {// 去掉标题栏this.requestWindowFeature(Window.FEATURE_NO_TITLE);//保持屏幕为横屏或者竖屏,禁止旋转setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏//将Acitivity 中的Window 的背景图设置为空,解决Android Activity切换时出现白屏问题getWindow().setBackgroundDrawable(null);//打印当前活动的activityLog.d(TAG, getClass().getSimpleName());// 知晓当前是在哪一个活动//3、获取屏幕宽和高DisplayMetrics metric = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metric);screenWidth = metric.widthPixels;// 屏幕宽度(像素)screenHeight = metric.heightPixels;//屏幕高度(像素) screenDendity = (int) metric.density;//屏幕密度比例3screenDendityDpi = metric.densityDpi;//屏幕密度480super.onCreate(savedInstanceState);} }
BaseFragment.java
package com.why.project.myapptemplet.fragment;import android.os.Bundle; import android.support.v4.app.Fragment;/*** Used 基础碎片类*/ public class BaseFragment extends Fragment {//传递过来的参数Bundle,供子类使用protected Bundle args;/*** 创建fragment的静态方法,方便传递参数* @param args 传递的参数* @return*/public static <T extends Fragment>T getInstance(Class clazz, Bundle args) {T mFragment=null;try {mFragment= (T) clazz.newInstance();} catch (java.lang.InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}mFragment.setArguments(args);return mFragment;}/*** 初始创建Fragment对象时调用* @param savedInstanceState*/@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);args = getArguments();} }
BaseDialogFragment.java
package com.why.project.myapptemplet.dialog;import android.support.v4.app.DialogFragment;/*** Created by HaiyuKing* Used*/public class BaseDialogFragment extends DialogFragment {}
参考资料
暂时空缺
项目demo下载地址
https://github.com/haiyuKing/MyAppTemplet