封装Activity基类,快速实现添加标题栏,设置状态栏样式

本篇博客是在网上学习快速封装一个ToolBar的基类的灵感,作了一定程度的衍生。
第一步:创建一个BaseActivity并在其布局文件中设定标题栏的样式,FrameLayout是用于装载子类Activity的布局。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary">
        <ImageButton
            android:scaleX="0.5"
            android:scaleY="0.5"
            android:id="@+id/left_btn"
            android:layout_width="50dp"
            android:layout_height="50dp"/>
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:layout_centerInParent="true"
            android:textSize="16sp"/>
        <ImageButton
            android:scaleX="0.5"
            android:scaleY="0.5"
            android:id="@+id/right_btn"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"/>
    RelativeLayout>
    <FrameLayout
        android:id="@+id/viewContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
LinearLayout>

界面当中的整个标题栏、左右侧按钮、标题名称都是可自定义的。

第二步:使用抽象方法的方式从子类Activity中收参,完成子类Activity界面的装载以及相关事件的配置。

public abstract class TopbarBaseActivity extends Activity{
    public static final int NO_STATUE_BAR = 0;
    public static final int ALPHA_STATUE_BAR = 1;
    public static final int NOMAL_STATUE_BAR = 2;
    private TextView wTitle;
    private RelativeLayout topBar;
    private ImageButton wLeft_btn;
    private ImageButton wRight_btn;
    private FrameLayout viewContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_baseactivity);
        wTitle = (TextView) findViewById(R.id.title);
        wLeft_btn = (ImageButton) findViewById(R.id.left_btn);
        wRight_btn = (ImageButton) findViewById(R.id.right_btn);
        viewContent = (FrameLayout) findViewById(R.id.viewContent);
        topBar = (RelativeLayout) findViewById(R.id.topbar);
        LayoutInflater.from(TopbarBaseActivity.this).inflate(getContentView(),viewContent);
        init(savedInstanceState);
    }
    protected abstract int getContentView();
    protected abstract void init(Bundle savedInstanceState);
    //设置状态栏样式
    protected void setStatueBar(int style){
        switch (style){
            case NO_STATUE_BAR:
                try {
                    if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
                        Window window = getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case ALPHA_STATUE_BAR:
                try {
                    if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
                        Window window = getWindow();
                        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }

    }
    //设置标题名称
    protected void setTopTitle(String topName , boolean show){
        if (show){
            if (!TextUtils.isEmpty(topName)){
                wTitle.setText(topName);
            }
        }else {
            topBar.setVisibility(View.GONE);
        }
    }
    //设置标题栏左侧的按钮
    protected void setLeftBtn(boolean show , int ResId , View.OnClickListener leftClick){
        if (show){
            wLeft_btn.setBackgroundResource(ResId);
            wLeft_btn.setOnClickListener(leftClick);
        }else {
            wLeft_btn.setVisibility(View.GONE);
        }
    }
    //设置标题栏右侧的按钮
    protected void setRightBtn(boolean show , int ResId , View.OnClickListener rightClick){
        if (show){
            wRight_btn.setBackgroundResource(ResId);
            wRight_btn.setOnClickListener(rightClick);
        }else {
            wRight_btn.setVisibility(View.GONE);
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            finish();
            overridePendingTransition(R.anim.left_fadein,R.anim.left_fadeout);
        }
        return true;
    }
    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.right_fadein,R.anim.right_fadeout);
    }
}

第三步:子类Activity继承此基类。使用姿势是这样的

public class ActivityMain extends TopbarBaseActivity {
    private Button cckk;
    @Override
    protected int getContentView() {
        return R.layout.activity_main;
    }
    @Override
    protected void init(Bundle savedInstanceState) {
        setStatueBar(TopbarBaseActivity.NO_STATUE_BAR);
        setTopTitle("主界面",false);
        cckk = (Button) findViewById(R.id.zhu);
        setLeftBtn(true, R.drawable.bbck, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkToast.getInstance("左侧按钮的点击事件").show();
            }
        });
        cckk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkToast.getInstance("测试点击事件").show();
            }
        });
        setRightBtn(false, R.drawable.menu, null);
    }
}

子类中可以配置标题栏的一系列属性,可以配置状态栏目的属性,核心思想是使用抽象方法完成回调的一个操作。

你可能感兴趣的:(Android开发)