android底部菜單欄的實現和百度地圖API的使用

本例子的代碼清單:

FirstActivity.java : 模擬應用程序啓動時的全屏載入效果;

SecondActivity.java : 程序主界面,實現底部菜單欄;

BaiduMap.java : 百度地圖API的調用例子。

工程的完整代碼,請到我的網盤下載:http://pan.baidu.com/share/link?shareid=267486539&uk=740495534

一、底部菜單欄的實現:

    在SecondActivity.java中調用兩個函數,onDrawBottomMenu()和setOnBottomMenuTouchListener()函數,前者用於繪製底部菜單欄,後者實現按鈕的點擊響應事件。

    在onDrawBottomMenu()函數中,

private void onDrawBottomMenu()
{
//get the resource of each button
Start = (TextView)findViewById(R.id.training);
History = (TextView)findViewById(R.id.history);
Weibo = (TextView)findViewById(R.id.weibo);
Location = (TextView)findViewById(R.id.myLocation);
//set the back ground image of each button
//Start.setBackgroundResource(R.drawable.start);
Start.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.start), null, null);
History.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.history), null, null);
Weibo.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.weibo), null, null);
Location.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.location), null, null);
Start.setTextSize(16);
History.setTextSize(16);
Weibo.setTextSize(16);
Location.setTextSize(16);
}

    如 line4~line7,使用的是TextView組件實現菜單欄的按鈕。通過setCompoundDrawablesWithIntrisicBounds()方法繪製按鈕背景圖,該函數還很方便各組件之間設置相對位置。

    佈局文件:

    

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/menu_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<!--android:drawableTop="@drawable/start" -->
<TextView
android:id="@+id/training"
android:text="@string/startTraining"
android:gravity="center_horizontal"
android:layout_alignParentLeft="true"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
</TextView>
<TextView
android:id="@+id/history"
android:text="@string/historyRecord"
android:gravity="center_horizontal"
android:layout_toRightOf="@id/training"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
</TextView>
<TextView
android:id="@+id/weibo"
android:text="@string/weibo"
android:gravity="center_horizontal"
android:layout_toRightOf="@id/history"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
</TextView>
<TextView
android:id="@+id/myLocation"
android:text="@string/myLocation"
android:gravity="center_horizontal"
android:layout_toRightOf="@id/weibo"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
</TextView>
</RelativeLayout>

二、百度地圖API的使用

    先到百度地圖API首頁下載相關的jar包,http://developer.baidu.com/map/,在“開發資源”中找到要下載的資源。

    將相關的jar文件複製到lib目錄下,如圖:

    將jar庫加入到工程中,單擊項目名稱,右鍵—>Properties—>Java Build Path—>Libraries—>Add JARs,將baidumapapi_v2_1_2.jar文件添加到工程中。如圖:

API的使用在BaiduMap.java文件中,有興趣的讀者可以下載看看。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章