首页 > 代码库 > Android中title的管理
Android中title的管理
常见的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(); }
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; } } }
效果基本是这样的:
下载地址:http://www.oschina.net/action/code/download?code=45216&id=63735
Android中title的管理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。