編輯:初級開發
在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顯示屬性,通過條件來設置其顯示
<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orIEntation="vertical"
android:paddingLeft="8dip"
android:paddingRight="12dip"
android:paddingTop="2dip"
android:paddingBottom="1dip"
android:background="@drawable/search_plate_browser" >
<ProgressBar android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="5dip"
android:max="100"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orIEntation="horizontal"
>
<LinearLayout android:id="@+id/title_bg"
android:background="@drawable/title_text"
android:layout_width="0dip"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orIEntation="horizontal"
>
<ImageVIEw android:id="@+id/favicon"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_marginLeft="3dip"
/>
<ImageVIEw android:id="@+id/lock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:visibility="gone"
/>
<TextVIEw
android:id="@+id/title"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1.0"
android:paddingLeft="8dip"
android:paddingRight="6dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black"
android:gravity="center_vertical"
android:singleLine="true"
android:ellipsize="end"
/>
</LinearLayout>
<ImageVIEw
android:id="@+id/rt_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="6dip"
android:scaleType="center"
android:layout_marginBottom="4dip"
android:background="@drawable/btn_bookmark"
android:src="@drawable/ic_btn_bookmarks"
/>
</LinearLayout>
</LinearLayout>
以下為 title_context.XML,裡面僅有兩條一個為分享該頁和復制本頁的URL
<menu XMLns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/title_bar_share_page_url"
android:title="@string/share_page"/>
<item android:id="@+id/title_bar_copy_page_url"
android:title="@string/copy_page_url"/>
</menu>
這是我在android下用OGL ES畫的第一個圖形,Render的override部分引用了其它同學的一些代碼 怎麼上截圖? Java代碼 public c
很多開發者考慮使自己的Android程序兼容多國語言,其實Google在設計Android時已經考慮了本地化問題,通過定義相關的資源可以自適應當前手機的語言來加載響應的
大家用eclipse調試android程序的時候可能遇到過不能顯示中文的問題,但有的時候顯示中文是非常必要的,尤其是對網絡數據進行解析的時候。這裡我給大家介紹一個簡單方
第一部分:arsc文件漢化目前市面上最流行的漢化方式,就是漢化這部分主文件。我們開始吧:首先,打開androidResEdit1.3然後把resources.arsc文