本例子的代码清单:
FirstActivity.java : 模拟应用程序启动时的全屏载入效果;
SecondActivity.java : 程序主界面,实现底部菜单栏;
BaiduMap.java : 百度地图API的调用例子。
一、底部菜单栏的实现:
在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()方法绘制按钮背景图,该函数还很方便各组件之间设置相对位置。
布局文件:
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:id="@+id/training"
android:text="@string/startTraining"
android:gravity="center_horizontal"
android:layout_alignParentLeft="true"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
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"
>
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"
>
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"
>
二、百度地图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文件中,有兴趣的读者可以下载看看。