《Android标题栏TitleBar全攻略.doc》由会员分享,可在线阅读,更多相关《Android标题栏TitleBar全攻略.doc(7页珍藏版)》请在金锄头文库上搜索。
1、【IT168资讯】很多细心的网友发现Android浏览器的标题栏TitleBar的功能比较多,细心的网友在查看Browser时会发现,从左到右依次为网站图标(favicon)、标题、最右边的动画进度条(圆圈)、背景进度条(和前面的不在一层),今天我们就一起来看看Android标题栏高级实现方法。在Android Browser程序中标题栏是自绘的,TitleBar类继承于线性布局LinearLayout类,通过LayoutInflater调用layout中的xml布局文件实现相关方法public class TitleBar extends LinearLayout private TextV
2、iew mTitle; /标题文字private Drawable mCloseDrawable;private ImageView mRtButton;private Drawable mCircularProgress; /圆圈进度指示private ProgressBar mHorizontalProgress; /水平进度条private ImageView mFavicon; /网站图标private ImageView mLockIcon;private Drawable mStopDrawable; /停止状态的图标private Drawable mBookmarkDrawab
3、le; /是一个书签的图标private boolean mInLoad;private BrowserActivity mBrowserActivity;private Drawable mGenericFavicon; /如果站点没有favicon.ico时显示的默认图标private int mIconDimension;private View mTitleBg; /文字的背景private MyHandler mHandler;private static int LONG_PRESS = 1;public TitleBar(BrowserActivity context) supe
4、r(context, null);mHandler = new MyHandler();LayoutInflater factory = LayoutInflater.from(context);factory.inflate(R.layout.title_bar, this); /从xml文件创建,android123提示大家,该文件的详细内容在本段代码最下方。mBrowserActivity = context;mTitle = (TextView) findViewById(R.id.title);mTitle.setCompoundDrawablePadding(5);mTitleBg
5、 = findViewById(R.id.title_bg);mLockIcon = (ImageView) findViewById(R.id.lock);mFavicon = (ImageView) findViewById(R.id.favicon);mRtButton = (ImageView) findViewById(R.id.rt_btn);Resources resources = context.getResources();mCircularProgress = (Drawable) resources.getDrawable(com.android.internal.R.
6、drawable.search_spinner);mIconDimension = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20f,resources.getDisplayMetrics();mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension);mHorizontalProgress = (ProgressBar) findViewById(R.id.progress_horizontal);mGenericFavicon = cont
7、ext.getResources().getDrawable(R.drawable.app_web_browser_sm);private class MyHandler extends Handler public void handleMessage(Message msg) if (msg.what = LONG_PRESS) mTitleBg.setPressed(false);mBrowserActivity.showTitleBarContextMenu();Overrideprotected void onCreateContextMenu(ContextMenu menu) /
8、创建上下文菜单,相关的xml代码在本文最后MenuInflater inflater = mBrowserActivity.getMenuInflater();inflater.inflate(R.menu.title_context, menu);Overridepublic boolean onTouchEvent(MotionEvent event) switch (event.getAction() case MotionEvent.ACTION_DOWN:if (int) event.getX() mTitleBg.getRight() mRtButton.setPressed(tr
9、ue); else mTitleBg.setPressed(true);mHandler.sendMessageDelayed(mHandler.obtainMessage(LONG_PRESS),ViewConfiguration.getLongPressTimeout();break;case MotionEvent.ACTION_MOVE:int slop = ViewConfiguration.get(mBrowserActivity).getScaledTouchSlop();if (int) event.getY() getHeight() + slop) mTitleBg.set
10、Pressed(false);mRtButton.setPressed(false);mHandler.removeMessages(LONG_PRESS);break;int x = (int) event.getX();int titleRight = mTitleBg.getRight();if (mTitleBg.isPressed() & x titleRight + slop) mTitleBg.setPressed(false);mHandler.removeMessages(LONG_PRESS); else if (mRtButton.isPressed() & x = mHorizontalProgress.getMax() mTitle.setCompoundDrawables(null, null, null, null);(Animatable) mCircularProgress).stop();mHorizontalProgress.setVisibility(View.INVISIBLE);if (mBookmarkDrawable != null) mRtButton.setImag