美文网首页
封装一个公共头部BaseHeadActivity

封装一个公共头部BaseHeadActivity

作者: 一克拉战英 | 来源:发表于2017-05-16 17:17 被阅读0次

首先我们先看下封装的基础类BaseHeadActivity.java类

/**
 * 简单返回的头部
 */

public class BaseHeadActivity extends AppCompatActivity {
    protected TextView titleTv;
    protected TextView titleRight;
    private ImageView titleBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_basehead_layout);
        initBaseView();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View.inflate(this, layoutResID, (ViewGroup) findViewById(R.id.base_content));
    }

    /**
     * 调用后 才能得到titleTv否则为空
     */
    private void initBaseView() {
        setSupportActionBar((Toolbar) findViewById(R.id.base_tool_bar));
        titleTv = (TextView) findViewById(R.id.base_toolbar_title);
        titleBack = (ImageView) findViewById(R.id.base_nav_back);
        titleRight = (TextView) findViewById(R.id.base_nav_right);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        titleRight.setEnabled(false);
        BaseTitleClick baseTitleClick = new BaseTitleClick();
        titleBack.setOnClickListener(baseTitleClick);
        titleRight.setOnClickListener(baseTitleClick);
    }

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

    /**
     * 返回事件
     */
    public void back() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 设置中间标题
     *
     * @param titleText
     */
    public void setTitle(String titleText) {
        if (titleText != null) {
            if (titleTv != null) {
                titleTv.setText(titleText);
            }
        }
    }


    /**
     * @param text
     * @param drawableRes 设置右侧的按钮,可显示文字或图片
     */
    public void setTitleRight(String text, Drawable drawableRes) {
        if (titleRight == null) {
            return;
        }
        if (text == null && drawableRes == null) {
            titleRight.setVisibility(View.GONE);
        } else {
            titleRight.setVisibility(View.VISIBLE);
        }
        if (text != null) {
            titleRight.setText(text);
            titleRight.setBackgroundResource(R.color.colorTransparents);
        }
        if (drawableRes != null) {
            titleRight.setBackgroundDrawable(drawableRes);
            titleRight.setText("");
        }

    }

    /**
     * 标题按钮的点击事件
     */
    private class BaseTitleClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.base_nav_back) {
                onBackClick();
            } else if (id == R.id.base_nav_right) {
                onRightClick();
            }
        }
    }

    /**
     * 标题中右边的部分,
     */
    protected void onRightClick() {
    }

    /**
     * 返回按钮的点击事件
     */
    private void onBackClick() {
        back();
    }

}

对应的xml文件activity_basehead_layout.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:background="#FFFFFF"
    android:orientation="vertical">
    <!--正文内容-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--头部-->
        <android.support.v7.widget.Toolbar
            android:id="@+id/base_tool_bar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/colorPrimaryDark"
            android:navigationContentDescription="back">

            <ImageView
                android:id="@+id/base_nav_back"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:paddingLeft="10dp"
                android:paddingRight="15dp"
                android:src="@mipmap/top_back" />

            <TextView
                android:id="@+id/base_toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:ellipsize="end"
                android:maxLines="1"
                android:textColor="@color/colorWhite"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/base_nav_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginRight="10dp"
                android:background="@color/colorAccent"
                android:clickable="true"
                android:textColor="@color/colorWhite"
                android:visibility="visible" />

        </android.support.v7.widget.Toolbar>
        <!--内容容器-->
        <FrameLayout
            android:id="@+id/base_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/base_tool_bar"/>

    </RelativeLayout>
</LinearLayout>

由于我们运用了ToolsBar,所以我们需要去掉系统的ActionBar效果,修改style.xml文件如下


    <style name="AppTheme" parent="AppBaseTheme">
    </style>

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/colorWhite</item>
        <item name="colorPrimaryDark">@color/colorBlack</item>
        <item name="colorAccent">@color/colorBlack</item>
        <item name="android:windowBackground">@android:color/white</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="actionBarSize">50dp</item>
        <item name="toolbarStyle">@style/ClubToolbar</item>
        <item name="toolbarNavigationButtonStyle">@style/MyToolbarNavigationButtonStyle</item>
    </style>
    <!--设置ToolBar的按钮样式-->
    <style name="MyToolbarNavigationButtonStyle" parent="Widget.AppCompat.Toolbar.Button.Navigation">
        <item name="android:minWidth">0dp</item>
        <item name="android:paddingLeft">10dp</item>
    </style>
    <!--设置ToolBar的边距-->
    <style name="ClubToolbar" parent="Widget.AppCompat.Toolbar">
        <item name="contentInsetStart">0dp</item>
        <item name="contentInsetEnd">0dp</item>
    </style>

封装好头部需要怎么应用呢


public class TestActivity extends BaseHeadActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        setTitle("首页");
        //设置右侧的图片
        setTitleRight(null,getResources().getDrawable(R.mipmap.ic_launcher));
        //如果右侧设置为文字
//        setTitleRight("保存",null);

    }

运用起来是不是很简单, 如果需要更改点击事件,重写下 BaseHeadActivity.java中的

   /**
     * 标题按钮的点击事件
     */
    private class BaseTitleClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.base_nav_back) {
                onBackClick();
            } else if (id == R.id.base_nav_right) {
                onRightClick();
            }
        }
    }

    /**
     * 标题中右边的部分,
     */
    protected void onRightClick() {
    }

    /**
     * 返回按钮的点击事件
     */
    private void onBackClick() {
        back();
    }

对应方法就可以了。最后上效果图

QQ图片20170516171926.jpg

相关文章

网友评论

      本文标题:封装一个公共头部BaseHeadActivity

      本文链接:https://www.haomeiwen.com/subject/mxwlxxtx.html