嘿,请执行以下步骤,我相信您的问题会解决:-
创建一个类HelloTabWidget.java
package com.pericent; //this is package name
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
public class HelloTabWidget extends TabActivity {
private String TAG="HelloTabWidget";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this,ArtistsActivity.class);
Log.v(TAG,"---artist activity is called---");
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this,AlbumsActivity.class);
Log.v(TAG,"---album activity is called---");
spec = tabHost.newTabSpec("albums").setIndicator("Albums",res.getDrawable(R.drawable.ic_tab_albums)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SongsActivity.class);
Log.v(TAG,"---song activity is called---");
spec = tabHost.newTabSpec("songs").setIndicator("Songs",res.getDrawable(R.drawable.ic_tab_songs)).setContent(intent);
tabHost.addTab(spec);
}
}
创建您的第二个活动:ArtistActivity.java
package com.pericent;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ArtistsActivity extends Activity {
private String TAG="ArtistsActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview=new TextView(this);
textview.setText("This is Artist Activity");
setContentView(textview);
Log.v(TAG,"---in artist activity---");
}
}
创建您的第三个活动:AlbumActivity.java
package com.pericent;
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class AlbumsActivity extends Activity{
private String TAG="AlbumsActivity";
@Override
public void onCreate(Bundle savedInstance)
{
super.onCreate(savedInstance);
TextView textview_album=new TextView(this);
textview_album.setText("This is album activity");
setContentView(textview_album);
Log.v(TAG,"---in album activity---");
}
}
创建您的第四个活动:SongsActivity.java
package com.pericent;
import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SongsActivity extends Activity{
private String TAG="SongsActivity";
@Override
public void onCreate(Bundle savedInstance)
{
super.onCreate(savedInstance);
TextView textview_song=new TextView(this);
textview_song.setText("This is song activity");
setContentView(textview_song);
Log.v(TAG,"---in songs activity---");
}
}
在 res/drawable 中创建一个文件夹
在此文件夹中创建 3 个 XML 文件:这些文件的代码如下:-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/ic_tab_artists_grey"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/ic_tab_artists_white" />
</selector>
在上面的 XML 代码中,我们使用了两个图像,以下是必须保存在同一文件夹(res/drawable)中的图像。
这是 main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/upper">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp">
<HorizontalScrollView android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillViewport="true" android:scrollbars="none">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</HorizontalScrollView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
</LinearLayout>
这是 AdroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pericent"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloTabWidget" android:label="@string/app_name" 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>
<activity android:name=".AlbumsActivity" android:label="@string/app_name"></activity>
<activity android:name=".ArtistsActivity" android:label="@string/app_name"></activity>
<activity android:name=".SongsActivity" android:label="@string/app_name" ></activity>
</application>
</manifest>
我认为所有这些信息都会有所帮助,如果您有任何问题,请随时问我任何问题。我总是很乐意帮助任何人。