技术开发 频道

Android标题栏TitleBar全攻略

  【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();

  }

  }

  };

  @Override

  protected void onCreateContextMenu(ContextMenu menu) { //创建上下文菜单,相关的xml代码在本文最后

  MenuInflater inflater = mBrowserActivity.getMenuInflater();

  inflater.inflate(R.menu.title_context, menu);

  }

  @Override

  public 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 < titleRight - slop) {

  mRtButton.setPressed(false);

  }

  break;

  case MotionEvent.ACTION_CANCEL:

  mRtButton.setPressed(false);

  mTitleBg.setPressed(false);

  mHandler.removeMessages(LONG_PRESS);

  break;

  case MotionEvent.ACTION_UP:

  if (mRtButton.isPressed()) {

  if (mInLoad) {

  mBrowserActivity.stopLoading();

  } else {

  mBrowserActivity.bookmarksOrHistoryPicker(false);

  }

  mRtButton.setPressed(false);

  } else if (mTitleBg.isPressed()) {

  mHandler.removeMessages(LONG_PRESS);

  mBrowserActivity.onSearchRequested();

  mTitleBg.setPressed(false);

  }

  break;

  default:

  break;

  }

  return true;

  }

  boolean isInLoad() {

  return mInLoad;

  }

  void setFavicon(Bitmap icon) {

  Drawable[] array = new Drawable[3];

  array[0] = new PaintDrawable(Color.BLACK);

  PaintDrawable p = new PaintDrawable(Color.WHITE);

  array[1] = p;

  if (icon == null) {

  array[2] = mGenericFavicon;

  } else {

  array[2] = new BitmapDrawable(icon);

  }

  LayerDrawable d = new LayerDrawable(array);

  d.setLayerInset(1, 1, 1, 1, 1);

  d.setLayerInset(2, 2, 2, 2, 2);

  mFavicon.setImageDrawable(d);

  }

  void setLock(Drawable d) {

  if (null == d) {

  mLockIcon.setVisibility(View.GONE);

  } else {

  mLockIcon.setImageDrawable(d);

  mLockIcon.setVisibility(View.VISIBLE);

  }

  }

  void setProgress(int newProgress) {

  if (newProgress >= 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.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 (mStopDrawable == 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 {

  mTitle.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 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2     android:layout_width="fill_parent"
3     android:layout_height="wrap_content"
4     android:orientation="vertical"
5     android:paddingLeft="8dip"
6     android:paddingRight="12dip"
7     android:paddingTop="2dip"
8     android:paddingBottom="1dip"
9     android:background="@drawable/search_plate_browser" >
10
11     <ProgressBar android:id="@+id/progress_horizontal"
12         style="?android:attr/progressBarStyleHorizontal"
13         android:layout_width="fill_parent"
14         android:layout_height="5dip"
15         android:max="100"
16         />
17
18     <LinearLayout
19         android:layout_width="fill_parent"
20         android:layout_height="wrap_content"
21         android:orientation="horizontal"
22         >
23
24         <LinearLayout android:id="@+id/title_bg"
25             android:background="@drawable/title_text"
26             android:layout_width="0dip"
27             android:layout_weight="1.0"
28             android:layout_height="wrap_content"
29             android:gravity="center_vertical"
30             android:orientation="horizontal"
31             >
32                 <ImageView android:id="@+id/favicon"
33                     android:layout_width="20dip"
34                     android:layout_height="20dip"
35                     android:layout_marginLeft="3dip"
36                     />
37                 <ImageView android:id="@+id/lock"
38                     android:layout_width="wrap_content"
39                     android:layout_height="wrap_content"
40                     android:layout_marginLeft="6dip"
41                     android:visibility="gone"
42                     />
43                 <TextView
44                     android:id="@+id/title"
45                     android:layout_height="wrap_content"
46                     android:layout_width="0dip"
47                     android:layout_weight="1.0"
48                     android:paddingLeft="8dip"
49                     android:paddingRight="6dip"
50                     android:textAppearance="?android:attr/textAppearanceMedium"
51                     android:textColor="@color/black"
52                     android:gravity="center_vertical"
53                     android:singleLine="true"
54                     android:ellipsize="end"
55                 />
56         </LinearLayout>
57         <ImageView
58             android:id="@+id/rt_btn"
59             android:layout_width="wrap_content"
60             android:layout_height="fill_parent"
61             android:layout_marginLeft="6dip"
62             android:scaleType="center"
63             android:layout_marginBottom="4dip"
64             android:background="@drawable/btn_bookmark"
65             android:src="@drawable/ic_btn_bookmarks"
66         />
67     </LinearLayout>
68 </LinearLayout>
69
70

  以下为 title_context.xml,里面仅有两条一个为分享该页和复制本页的URL

 

1 <menu xmlns:android="http://schemas.android.com/apk/res/android">
2     <item android:id="@+id/title_bar_share_page_url"
3         android:title="@string/share_page"/>
4     <item android:id="@+id/title_bar_copy_page_url"
5         android:title="@string/copy_page_url"/>
6 </menu>
7
0
相关文章