《android标题栏titlebar全攻略》由会员分享,可在线阅读,更多相关《android标题栏titlebar全攻略(7页珍藏版)》请在金锄头文库上搜索。
1、【IT168 资讯】很多细心的网友发现 Android浏览器的标题栏 TitleBar的功能比较多,细心的网友在查看 Browser时会发现,从左到右依次为网站图标(favicon)、标题、最右边的动画进度条(圆圈)、背景进度条(和前面的不在一层),今天我们就一起来看看 Android标题栏高级实现方法。在 Android Browser程序中标题栏是自绘的,TitleBar 类继承于线性布局LinearLayout类,通过 LayoutInflater调用 layout中的 xml布局文件实现相关方法public class TitleBar extends LinearLayout pri
2、vate TextView mTitle; /标题文字private Drawable mCloseDrawable;private ImageView mRtButton;private Drawable mCircularProgress; /圆圈进度指示private ProgressBar mHorizontalProgress; /水平进度条private ImageView mFavicon; /网站图标private ImageView mLockIcon;private Drawable mStopDrawable; /停止状态的图标private Drawable mBook
3、markDrawable; /是一个书签的图标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 co
4、ntext) super(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.setCompoundDrawablePaddin
5、g(5);mTitleBg = 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.androi
6、d.internal.R.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);mGenericF
7、avicon = context.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(Contex
8、tMenu menu) /创建上下文菜单,相关的 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
9、.setPressed(true); 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
10、) mTitleBg.setPressed(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.isPres
11、sed() & x = mHorizontalProgress.getMax() mTitle.setCompoundDrawables(null, null, null, null);(Animatable) mCircularProgress).stop();mHorizontalProgress.setVisibility(View.INVISIBLE);if (mBookmarkDrawable != null) mRtButton.setImageDrawable(mBookmarkDrawable);mInLoad = false; else mHorizontalProgress
12、.setProgress(newProgress);if (!mInLoad & getWindowToken() != null) mTitle.setCompoundDrawables(null, null, mCircularProgress,null);(Animatable) mCircularProgress).start();mHorizontalProgress.setVisibility(View.VISIBLE);if (mBookmarkDrawable = null) mBookmarkDrawable = mRtButton.getDrawable();if (mSt
13、opDrawable = null) mRtButton.setImageResource(R.drawable.ic_btn_stop_v2);mStopDrawable = mRtButton.getDrawable(); else mRtButton.setImageDrawable(mStopDrawable);mInLoad = true;void setTitleAndUrl(CharSequence title, CharSequence url) if (url = null) mTitle.setText(R.string.title_bar_loading); else m
14、Title.setText(url.toString();void setToTabPicker() mTitle.setText(R.string.tab_picker_title);setFavicon(null);setLock(null);mHorizontalProgress.setVisibility(View.GONE);本文的相关的 title_bar.xml布局文件内容,其中大家可以理解下部分图标在创建时使用隐藏 GONE的 visibility显示属性,通过条件来设置其显示1 10 11 17 18 23 24 32 37 43 56 57 67 68 69 70以下为 title_context.xml,里面仅有两条一个为分享该页和复制本页的 URL1 2 4 6 7