个人完成案例之乐学成语(显示主界面)
测试篇完成了两个测试类,现在我们就要开始编写主要的程序代码了。
显示主界面
- 在res的drawable-hdpi目录下拷入需要的图片素材,在res/layout目录中新建activity_main.xml布局,代码如下;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/tab1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout> <LinearLayout android:id="@+id/tab3" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </RelativeLayout>
- 在res的values目录的strings.xml文件中定义所需的字符串;
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">HappyIdiom</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="title_activity_main">MainActivity</string> <string name="title_study">学习</string> <string name="title_search">搜搜</string> <string name="title_game">游戏</string> <string name="title_save">收藏</string> <string name="title_help">帮助</string>
</resources>
- 在activity包下新建MainActivity继承自Activity(注意取消标题栏的方法一定要位于setContentView()方法之前);
package cn.edu.bztc.happyidiom.activity; import com.example.happyidiom.R; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.Window; import android.widget.TabHost; public class MainActivity extends TabActivity { private TabHost tabHost; protected void onCreate(Bundle saveInstanceState) { super.onCreate(saveInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);// 取消标题栏 setContentView(R.layout.activity_main); tabHost = getTabHost(); addTab("study", R.string.title_study, R.drawable.study, R.id.tab1); addTab("search", R.string.title_search, R.drawable.search, R.id.tab2); addTab("game", R.string.title_game, R.drawable.game, R.id.tab3); addTab("save", R.string.title_save, R.drawable.save, R.id.tab1); addTab("help", R.string.title_help, R.drawable.help, R.id.tab2); } private void addTab(String tag, int title_introduction, int title_icon, int content) { // TODO Auto-generated method stub tabHost.addTab(tabHost .newTabSpec(tag) .setIndicator(getString(title_introduction), getResources().getDrawable(title_icon)) .setContent(content)); } public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.main, menu); return true; } }
- 配置AndriodManifest.xml文件。AndriodManifest.xml是项目的总配置文件,记录应用中所使用的各种组件。这个文件列出了应用程序所提供的功能,在这个文件中,你可以指定应用程序使用到的服务(如电话服务、互联网 服务、短信服务、GPS服务等等)。另外当你新添加一个Activity的时候,也需要在这个文件中进行相应配置,只有配置好后,才能调用此 Activity。AndroidManifest.xml将包含如下设置:application permissions、Activities、intent filters等。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.happyidiom" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="cn.edu.bztc.happyidiom.activity.MainActivity" android:label="@string/title_activity_main" android:theme="@android:style/Theme.NoTitleBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="android.test.runner" /> </application> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.edu.bztc.happyidiom" > </instrumentation> </manifest>
程序的运行效果如图:
- 运行过程中出现错误:
- 解决方法:
- 因为之前写MainActivity时用的是系统自动生成的包,不是在activity包下建立的,所以系统找不到com.example.happyidiom.activity包的路径,打开AndroidManifest.xml文件,将com.example.happyidiom.activity包名改为com.example.happyidiom,保存之后运行成功。