常见的Android应用每个Activity或者Fragment都会带有一个Title栏。最普通的就是每个Activity的布局文件中都写一个title。但是这样管理起来比较麻烦,因为每个activity的title栏其实看起来都差不多,基本都包含有返回功能,一个textview描述当前页面,最右侧会是一个更多或者搜索之类的功能按钮。这样我们其实可以使用一个通用的布局,在所有的activity的父类中直接控制title的样式。
1.首先是BaseActivity作为所有Activity的父类
public abstract class BaseActivity extends Activity { public LayoutInflater inflater; public Resources mResources; public Context mContext; public View rootView; private View titleView; private LinearLayout.LayoutParams params; public int layoutId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); inflater = getLayoutInflater(); layoutId = getContentView(); rootView = inflater.inflate(layoutId, null); mContext = this; mResources = mContext.getResources(); params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) mResources.getDimension(R.dimen.common_title_height)); setContentView(rootView); findViewById(); initTitleView(); initData(); } private void initTitleView() { titleView = TitleManager.getInstance(this).initTitleView(layoutId); ((ViewGroup) rootView).addView(titleView, 0, params); } /** * 绑定控件id */ protected abstract void findViewById(); protected abstract void initData(); /** * 初始化控件 */ protected abstract int getContentView(); }
2.接着是titlemanager类
public class TitleManager implements View.OnClickListener { private static TitleManager uniqueInstance = null; private Activity context; private View titleView; private LayoutInflater inflater; private TitleManager(Activity context) { this.context = context; inflater = context.getLayoutInflater(); } public static TitleManager getInstance(Activity context) { if (uniqueInstance == null) { uniqueInstance = new TitleManager(context); } return uniqueInstance; } public ImageView mCommonTitleLeft; public TextView mCityNameTextView; public TextView mTitleTextView; public ImageView mSearch; public ImageView mFavorite; public ImageView mShare; public View initTitleView(int layoutId) { titleView = inflater.inflate(R.layout.view_product_common_title, null); mCommonTitleLeft = (ImageView) titleView.findViewById(R.id.common_title_left); mCommonTitleLeft.setOnClickListener(this); mCityNameTextView = (TextView) titleView.findViewById(R.id.activity_product_home_left); mCityNameTextView.setOnClickListener(this); mTitleTextView = (TextView) titleView.findViewById(R.id.common_title_center); mSearch = (ImageView) titleView.findViewById(R.id.common_title_search); mSearch.setOnClickListener(this); mFavorite = (ImageView) titleView.findViewById(R.id.details_product_favorite_img); mFavorite.setOnClickListener(this); mShare = (ImageView) titleView.findViewById(R.id.details_product_shear_img); mShare.setOnClickListener(this); switch (layoutId) { case R.layout.main: mCityNameTextView.setVisibility(View.VISIBLE); mSearch.setVisibility(View.VISIBLE); mTitleTextView.setText(R.string.title_activity_main); break; case R.layout.main1: mCommonTitleLeft.setVisibility(View.VISIBLE); mTitleTextView.setText(R.string.title_activity_main1); break; case R.layout.main2: mCommonTitleLeft.setVisibility(View.VISIBLE); mFavorite.setVisibility(View.VISIBLE); mTitleTextView.setText(R.string.title_activity_main2); break; case R.layout.main3: mCommonTitleLeft.setVisibility(View.VISIBLE); mShare.setVisibility(View.VISIBLE); mTitleTextView.setText(R.string.title_activity_main3); break; default: mCityNameTextView.setVisibility(View.GONE); mSearch.setVisibility(View.GONE); mCommonTitleLeft.setVisibility(View.VISIBLE); mFavorite.setVisibility(View.GONE); mShare.setVisibility(View.GONE); break; } return titleView; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.common_title_left: context.finish(); break; case R.id.common_title_search: Toast.makeText(context, "search", Toast.LENGTH_SHORT).show(); break; } } }
3.Activity和xml布局文件就不给了,在后面直接放上demo,里面包含的整个工程。
效果基本是这样的:
下载地址:http://www.oschina.net/action/code/download?code=45216&id=63735
时间: 2024-10-25 09:04:52