资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
【IT168资讯】很多细心的网友发现Android浏览器的标题栏TitleBar的功能比较多,细心的网友在查看Browser时会发现,从左到右依次为网站图标(favicon)、标题、最右边的动画进度条(圆圈)、背景进度条(和前面的不在一层),今天我们就一起来看看Android标题栏高级实现方法。在Android Browser程序中标题栏是自绘的,TitleBar类继承于线性布局LinearLayout类,通过LayoutInflater调用layout中的xml布局文件实现相关方法public class TitleBar extends LinearLayout private 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 mBookmarkDrawable; /是一个书签的图标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) 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.setCompoundDrawablePadding(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.android.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);mGenericFavicon = 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(ContextMenu 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.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) 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.isPressed() & x = mHorizontalProgress.getMax() mTitle.setCompoundDrawables(null, null, null, null);(Animatable) mCircularProgress).stop();mHorizontalProgress.setVisibility(View.INVISIBLE);if (mBookmarkDrawable != null) mRtButton.setImag
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号