http://www.ophonesdn.com/forum/viewthread.jsp?tid=2188
很多细心的网友发现Android浏览器的标题栏TitleBar的功能比较多,细心的网友在查看Browser时会发现,从左到右依次为网站图标 (favicon)、标题、最右边的动画进度条(圆圈)、背景进度条(和前面的不在一层),今天我们就一起来看看Android标题栏高级实现方法。 在Android Browser程序中标题栏是自绘的,TitleBar类继承于线性布局LinearLayout类,通过LayoutInflater调用layout 中的xml布局文件实现相关方法
01.public class TitleBar extends LinearLayout {02. 03.private TextView mTitle; //标题文字04. 05.private Drawable mCloseDrawable;06. 07.private ImageView mRtButton;08. 09.private Drawable mCircularProgress; //圆圈进度指示10. 11.private ProgressBar mHorizontalProgress; //水平进度条12. 13.private ImageView mFavicon; //网站图标14. 15.private ImageView mLockIcon;16. 17.private Drawable mStopDrawable; //停止状态的图标18. 19.private Drawable mBookmarkDrawable; //是一个书签的图标20. 21.private boolean mInLoad;22. 23.private BrowserActivity mBrowserActivity;24. 25.private Drawable mGenericFavicon; //如果站点没有favicon.ico时显示的默认 图标26. 27.private int mIconDimension;28. 29. private View mTitleBg; //文字的背景30. 31.private MyHandler mHandler;32. 33.private static int LONG_PRESS = 1;34. 35.public TitleBar(BrowserActivity context) {36. 37.super(context, null);38. 39.mHandler = new MyHandler();40. 41.LayoutInflater factory = LayoutInflater.from(context);001.mBrowserActivity = context;002. 003.mTitle = (TextView) findViewById(R.id.title);004. 005.mTitle.setCompoundDrawablePadding(5);006. 007.mTitleBg = findViewById(R.id.title_bg);008. 009.mLockIcon = (ImageView) findViewById(R.id.lock);010. 011.mFavicon = (ImageView) findViewById(R.id.favicon);012. 013.mRtButton = (ImageView) findViewById(R.id.rt_btn);014. 015.Resources resources = context.getResources();016. 017.mCircularProgress = (Drawable) resources.getDrawable(com.android.internal.R.drawable.search_spinner);018. 019.mIconDimension = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20f,resources.getDisplayMetrics());020. 021. mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension);022. 023.mHorizontalProgress = (ProgressBar) findViewById(R.id.progress_horizontal);024. 025.mGenericFavicon = context.getResources().getDrawable(R.drawable.app_web_browser_sm);026. 027.}028. 029.private class MyHandler extends Handler {030. 031.public void handleMessage(Message msg) {032. 033.if (msg.what == LONG_PRESS) {034. 035.mTitleBg.setPressed(false);036. 037. mBrowserActivity.showTitleBarContextMenu();038. 039.}040. 041.}042. 043. };044. 045.@Override046. 047.protected void onCreateContextMenu(ContextMenu menu) { //创建上下文菜单,相关的xml代码在本文最后048. 049. MenuInflater inflater = mBrowserActivity.getMenuInflater();050. 051. inflater.inflate(R.menu.title_context, menu);052. 053.}054. 055. @Override056. 057.public boolean onTouchEvent(MotionEvent event) {058. 059.switch (event.getAction()) {060. 061.case MotionEvent.ACTION_DOWN:062. 063.if ((int) event.getX() > mTitleBg.getRight()) {064. 065. mRtButton.setPressed(true);066. 067.} else {068. 069. mTitleBg.setPressed(true);070. 071. mHandler.sendMessageDelayed(mHandler.obtainMessage(072. 073.LONG_PRESS),074. 075.ViewConfiguration.getLongPressTimeout());076. 077.}078. 079.break;080. 081.case MotionEvent.ACTION_MOVE:082. 083.int slop = ViewConfiguration.get(mBrowserActivity)084. 085..getScaledTouchSlop();086. 087.if ((int) event.getY() > getHeight() + slop) {088. 089. mTitleBg.setPressed(false);090. 091.mRtButton.setPressed(false);092. 093.mHandler.removeMessages(LONG_PRESS);094. 095.break;096. 097.}098. 099. int x = (int) event.getX();100. 101.int titleRight = mTitleBg.getRight();102. 103.if (mTitleBg.isPressed() && x > titleRight + slop) {104. 105.mTitleBg.setPressed(false);106. 107. mHandler.removeMessages(LONG_PRESS);108. 109.} else if (mRtButton.isPressed() && x < titleRight - slop) {110. 111. mRtButton.setPressed(false);112. 113.}114. 115.break;116. 117.case MotionEvent.ACTION_CANCEL:118. 119.mRtButton.setPressed(false);120. 121. mTitleBg.setPressed(false);122. 123.mHandler.removeMessages(LONG_PRESS);124. 125.break;126. 127.case MotionEvent.ACTION_UP:128. 129.if (mRtButton.isPressed()) {130. 131.if (mInLoad) {132. 133. mBrowserActivity.stopLoading();134. 135.} else {136. 137. mBrowserActivity.bookmarksOrHistoryPicker(false);138. 139.}140. 141. mRtButton.setPressed(false);142. 143.} else if (mTitleBg.isPressed()) {144. 145.mHandler.removeMessages(LONG_PRESS);146. 147. mBrowserActivity.onSearchRequested();148. 149. mTitleBg.setPressed(false);150. 151.}152. 153.break;154. 155.default:156. 157.break;158. 159.}160. 161.return true;162. 163.}164. 165.boolean isInLoad() {166. 167.return mInLoad;168. 169.}170. 171.void setFavicon(Bitmap icon) {172. 173.Drawable[] array = new Drawable[3];174. 175.array[0] = new PaintDrawable(Color.BLACK);176. 177.PaintDrawable p = new PaintDrawable(Color.WHITE);178. 179.array[1] = p;180. 181.if (icon == null) {182. 183.array[2] = mGenericFavicon;184. 185.} else {186. 187. array[2] = new BitmapDrawable(icon);188. 189.}190. 191.LayerDrawable d = new LayerDrawable(array);192. 193.d.setLayerInset(1, 1, 1, 1, 1);194. 195.d.setLayerInset(2, 2, 2, 2, 2);196. 197.mFavicon.setImageDrawable(d);198. 199.}200. 201.void setLock(Drawable d) {202. 203.if (null == d) {204. 205. mLockIcon.setVisibility(View.GONE);206. 207.} else {208. 209. mLockIcon.setImageDrawable(d);210. 211. mLockIcon.setVisibility(View.VISIBLE);212. 213.}214. 215.}216. 217.void setProgress(int newProgress) {218. 219.if (newProgress >= mHorizontalProgress.getMax()) {220. 221. mTitle.setCompoundDrawables(null, null, null, null);222. 223. ((Animatable) mCircularProgress).stop();224. 225. mHorizontalProgress.setVisibility(View.INVISIBLE);226. 227.if (mBookmarkDrawable != null) {228. 229. mRtButton.setImageDrawable(mBookmarkDrawable);230. 231.}232. 233.mInLoad = false;234. 235.} else {236. 237. mHorizontalProgress.setProgress(newProgress);238. 239.if (!mInLoad && getWindowToken() != null) {240. 241. mTitle.setCompoundDrawables(null, null, mCircularProgress,242. 243. null);244. 245.((Animatable) mCircularProgress).start();246. 247. mHorizontalProgress.setVisibility(View.VISIBLE);248. 249.if (mBookmarkDrawable == null) {250. 251.mBookmarkDrawable = mRtButton.getDrawable();252. 253.}254. 255.if (mStopDrawable == null) {256. 257.mRtButton.setImageResource(R.drawable.ic_btn_stop_v2);258. 259. mStopDrawable = mRtButton.getDrawable();260. 261.} else {262. 263. mRtButton.setImageDrawable(mStopDrawable);264. 265.}266. 267.mInLoad = true;268. 269.}270. 271.}272. 273.}274. 275.void setTitleAndUrl(CharSequence title, CharSequence url) {276. 277.if (url == null) {278. 279.mTitle.setText(R.string.title_bar_loading);280. 281.} else {282. 283.mTitle.setText(url.toString());284. 285.}286. 287.}288. 289.void setToTabPicker() {290. 291. mTitle.setText(R.string.tab_picker_title);292. 293.setFavicon(null);294. 295.setLock(null);296. 297.mHorizontalProgress.setVisibility(View.GONE);298. 299.}300. 301.}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
posted on
浙公网安备 33010602011771号