package newdemo.jeno.designdemo.activitynew; import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.View;import android.widget.ImageView;import android.widget.TextView; import newdemo.jeno.designdemo.R;import newdemo.jeno.designdemo.view.ToolBarBean; /** * Created by songjiabin1 on 2016/9/18. */public abstract class BaseActivity extends AppCompatActivity { private TextView tvTitle; private ImageView ivTopBack; private Toolbar toolbar; private ToolBarBean toolBarBean; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //此为积累的activity setContentView(getLayout()); inintData(); } /** * 得到布局文件 * * @return */ public abstract int getLayout(); /** * 初始化view */ protected abstract void inintData(); protected <T extends View> T findView(int resourcesId) { return (T) findViewById(resourcesId); } protected ToolBarBean getToolbar() { if (toolBarBean == null) { toolbar = (Toolbar) findViewById(R.id.toolbar); toolBarBean = new ToolBarBean(this, toolbar); } return toolBarBean; } }
package newdemo.jeno.designdemo.view; import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.View;import android.widget.ImageView;import android.widget.TextView; import newdemo.jeno.designdemo.R; /** * Created by songjiabin1 on 2016/9/18. */public class ToolBarBean { private TextView tvTitle; private ImageView ivToolbarBack; private AppCompatActivity context; private Toolbar toolbar; public ToolBarBean(AppCompatActivity context, Toolbar toolbar) { this.context = context; this.toolbar = toolbar; ivToolbarBack = (ImageView) toolbar.findViewById(R.id.iv_topBack); tvTitle = (TextView) toolbar.findViewById(R.id.tvTitle); } /** * 进行回退 */ public ToolBarBean setBack() { ivToolbarBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { context.finish(); } }); return this; } /** * 设置标题 */ public ToolBarBean setTitle(String title) { tvTitle.setText(title); return this; } /** * 自定义的回退 */ public ToolBarBean setCustomBack(View.OnClickListener onClickListener) { ivToolbarBack.setOnClickListener(onClickListener); return this; }}
时间: 2024-10-02 14:20:13