前言
最近学习Android开发,公司马上要有Android项目开发,突感压力山大。接到上级命令,做个类似新浪微博的界面出来,只好加紧学习,搜集资料,终于不负圣恩,按时完成。由此促成这篇文章的诞生,废话不多说,上正文.......
AndroidManifest.xml
简介
Android程序的主入口,类似于J2EE项目中的web.xml,定义了程序的内容和行为。
代码
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.duanyr.sinaweibotabhost"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Android程序主入口,调用MainActivity -->
<activity
android:name=".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>
<!-- 注册项目用到的多个Activity -->
<activity android:name=".HomeActivity" />
<activity android:name=".MessageActivity" />
<activity android:name=".InfoActivity" />
</application>
</manifest>
由上述配置文件可以看出,程序的主入口为MainActivity.java,接下来看MainActivity.java的内容。
MainActivity.java
package com.duanyr.sinaweibotabhost;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;
/**
* @author: duanyr
* @创建时间: 2012-11-12 下午4:02:00
*
* 类说明:仿新浪微博底部TabHost实现
*/
public class MainActivity extends TabActivity implements
OnCheckedChangeListener {
private TabHost tabHost;
private Intent homeIntent;
private Intent messageIntent;
private Intent infoIntent;
/**
* 重写Activity的onCreate方法
* 该方法当程序启动时,切换横竖屏时,再次启动时运行
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//不显示title
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// ~~~~~~~~~~~~ 初始化~~~~~~~~~~//
this.homeIntent = new Intent(this, HomeActivity.class);
this.messageIntent = new Intent(this, MessageActivity.class);
this.infoIntent = new Intent(this, InfoActivity.class);
initRadios();//初始化底部按钮
setupIntent();//绑定选项卡数据
}
/**
* 初始化底部按钮
*/
private void initRadios() {
((RadioButton) findViewById(R.id.radio_btn_home))
.setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.radio_btn_message))
.setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.radio_btn_info))
.setOnCheckedChangeListener(this);
}
/**
* 底部按钮改变是调用的方法
*/
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
switch (buttonView.getId()) {
case R.id.radio_btn_home:
this.tabHost.setCurrentTabByTag("home_tab");
break;
case R.id.radio_btn_message:
this.tabHost.setCurrentTabByTag("message_tab");
break;
case R.id.radio_btn_info:
this.tabHost.setCurrentTabByTag("info_tab");
break;
}
}
}
/**
* 绑定各个选项卡数据内容
*/
private void setupIntent() {
this.tabHost = getTabHost();
TabHost localTabHost = this.tabHost;
localTabHost.addTab(buildTabSpec("home_tab", R.string.main_home,
R.drawable.icon_home, this.homeIntent));
localTabHost.addTab(buildTabSpec("message_tab", R.string.main_message,
R.drawable.icon_message, this.messageIntent));
localTabHost.addTab(buildTabSpec("info_tab", R.string.main_info,
R.drawable.icon_info, this.infoIntent));
}
private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
final Intent content) {
return this.tabHost
.newTabSpec(tag)
.setIndicator(getString(resLabel),
getResources().getDrawable(resIcon))
.setContent(content);
}
}
由上述文件可以看出,其加载的视图为activity_main.xml,接下来看activity_main.xml。
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:visibility="gone" />
<RadioGroup
android:id="@+id/main_radio"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/maintab_bg"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio_btn_home"
style="@style/main_tab_bottom"
android:layout_marginTop="2.0dip"
android:checked="true"
android:drawableTop="@drawable/icon_home"
android:text="@string/main_home" />
<RadioButton
android:id="@+id/radio_btn_message"
style="@style/main_tab_bottom"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/icon_message"
android:text="@string/main_message" />
<RadioButton
android:id="@+id/radio_btn_info"
style="@style/main_tab_bottom"
android:layout_marginTop="2.0dip"
android:drawableTop="@drawable/icon_info"
android:text="@string/main_info" />
</RadioGroup>
</LinearLayout>
</TabHost>
由上述xml文件可以看出,此底部菜单是有一个单选按钮组来完成的,其中包括三个单选按钮,分别为home(主页)、message(消息)、info(个人信息),结合MainActivity.java可以知道当改变单选按钮时,会显示不同的视图。以message为例,MessageActivity.java如下
MessageActivity.java
package com.duanyr.sinaweibotabhost;
import android.app.Activity;
import android.os.Bundle;
public class MessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
}
}
activity_message.xml
<LinearLayout 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" >
<TextView
android:id="@+id/my_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_message" />
</LinearLayout>
以上为Message(信息)对应的Activity和XML文件,其他两个也于此一样,就不在一一列举了,接下来是项目中的资源文件:String.xml和styles.xml
String.xml
<resources>
<string name="app_name">SinaWeiboTabHost</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="main_home">主页</string>
<string name="main_message">消息</string>
<string name="main_info">个人资料</string>
</resources>
Styles.xml
<resources>
<style name="AppTheme" parent="android:Theme.Light" />
<style name="main_tab_bottom">
<item name="android:textSize">10.0dip</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:ellipsize">marquee</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@drawable/home_btn_bg</item>
<item name="android:paddingTop">5.0dip</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item>
<item name="android:singleLine">true</item>
<item name="android:drawablePadding">2.0dip</item>
<item name="android:layout_weight">1.0</item>
</style>
</resources>
还有部分图片资源不方便上传,具体项目已经上传到资源库中,地址为: http://download.youkuaiyun.com/detail/duanyanrui/4759463,欢迎大家下载。此代码有借鉴网友的代码资料,在此谢谢各位网友的不吝赐教,项目有不当之处还请大家多多指教。