乐学成语——显示主界面

来源:互联网 发布:windows搭建免流 编辑:程序博客网 时间:2024/05/16 18:56

     1.首先在res的drawable-hdpi目录下拷入需要的图片素材。

     2.在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"    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>
  布局文件中的内容比较简单,主要拖了一个TabHost控件到界面上,如图:


     3.在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="animal">动物</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>
     4.在Activity包下新建MainActivity继承自Activity,代码如下:

public class MainActivity extends TabActivity {private TabHost tabHost;protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏setContentView(R.layout.activity_main);tabHost=getTabHost();addTab("study",R.string.title_study,R.drawable.search,R.id.tab1<span style="font-family: Arial, Helvetica, sans-serif;">);</span>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.search,R.id.tab2);}private void addTab(String tag,int title_introduction,int title_icon,int content){tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(getString(title_introduction), getResources().getDrawable(title_icon)).setContent(content));}public boolean onCreateOptionsMenu(Menu menu){//inflate the menu;this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
  在这个类的onCreate()方法里,通过调用getTabHost()方法来获取整个TabHost()组件,然后调用了抽取出来的自定义的方法addTab()添加了五个选项卡。方法的四个参数分别为每个选项卡的tag,指示器上显示的标题,指示器上显示的图片,选项卡对应的内容。

  注意取消标题栏的方法,一定要位于setContentView()方法之前。

     5.配置AndroidManifest.xml文件。

<activity            android:name="cn.edu.bztc.happyidiom.activity.MainActivity"            android:label="@string/title_activity_main">            <intent-filter>                            <action android:name="android.intent.action.MAIN" />                               <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>        </activity>
  主要是注册了MainActivity,通过加入<intent-filte>将其设置为首先启动的类。运行程序,如图:

  让图片显示出来,在AndroidManifest.xml更改配置:android:theme="@android:style/Theme.NoTitleBar",结果如图所示:





0 0