当我将它添加到我的xml中时,我试图使用tabhost它看起来不正确,我相信有一些东西需要在java中完成,我试图设置三个不同类的三个选项卡这有可能吗?
这是我的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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<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:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Browser History:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call Log"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/call"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Messages"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvSms"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
</LinearLayout>
我有三个不同的类,因为我试图使用每个选项卡打开每个活动。
以下是课程
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.widget.TabHost;
import android.widget.TextView;
public class AndroidSpybetaActivity extends TabActivity {
/** Called when the activity is first created. */
Resources res;
TabHost tabHost;
Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
intent = new Intent().setClass(this, Messaging.class);
spec = tabHost.newTabSpec("messaging").setIndicator("Messaging")
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TestingData.class);
spec = tabHost.newTabSpec("Calls").setIndicator("Calls")
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
TextView view = (TextView) findViewById(R.id.hello);
Cursor mCur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
null, null, null, null);
mCur.moveToFirst();
int index = mCur.getColumnIndex(Browser.BookmarkColumns.TITLE);
while (mCur.isAfterLast() == false) {
view.append(" WebSite " + mCur.getString(index));
mCur.moveToNext();
}
}
}
第二
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Messaging extends TabActivity{
static TextView messageBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
messageBox = (TextView) findViewById(R.id.tvSms);
}
public static void updateMessageBox(String msg) {
messageBox.append(msg);
}
}
和第三
package com.johnnydicamillo.spybeta;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.widget.TextView;
public class TestingData extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.id.call);
String[] projection = new String[] {
Calls.NUMBER
};
Cursor mCur = managedQuery(CallLog.Calls.CONTENT_URI, projection,
Calls.DURATION + "<?", new String[] { "60" }, Calls.DURATION
+ " ASC");
mCur.moveToFirst();
while (mCur.isAfterLast() == false) {
for (int i = 0; i < mCur.getColumnCount(); i++) {
view.append(" Number " + mCur.getString(i));
}
mCur.moveToNext();
}
}
}
这是我的logcat:
08-12 15:19:16.368: D/AndroidRuntime(280): Shutting down VM
08-12 15:19:16.368: W/dalvikvm(280): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-12 15:19:16.628: E/AndroidRuntime(280): FATAL EXCEPTION: main
08-12 15:19:16.628: E/AndroidRuntime(280): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.AndroidSpybetaActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.Messaging}; have you declared this activity in your AndroidManifest.xml?
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.os.Looper.loop(Looper.java:123)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-12 15:19:16.628: E/AndroidRuntime(280): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 15:19:16.628: E/AndroidRuntime(280): at java.lang.reflect.Method.invoke(Method.java:521)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-12 15:19:16.628: E/AndroidRuntime(280): at dalvik.system.NativeStart.main(Native Method)
08-12 15:19:16.628: E/AndroidRuntime(280): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.johnnydicamillo.spybeta/com.johnnydicamillo.spybeta.Messaging}; have you declared this activity in your AndroidManifest.xml?
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.resolveActivityInfo(ActivityThread.java:2473)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:277)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.widget.TabHost.addTab(TabHost.java:213)
08-12 15:19:16.628: E/AndroidRuntime(280): at com.johnnydicamillo.spybeta.AndroidSpybetaActivity.onCreate(AndroidSpybetaActivity.java:31)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-12 15:19:16.628: E/AndroidRuntime(280): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-12 15:19:16.628: E/AndroidRuntime(280): ... 11 more
08-12 15:19:21.929: I/Process(280): Sending signal. PID: 280 SIG: 9
答案 0 :(得分:1)
是的,这是可能的。
您可以按以下方式为每个标签指定每个活动(开始一个意图)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
intent = new Intent().setClass(this, CalendarActivity.class);
spec = tabHost.newTabSpec("calendar").setIndicator("Calendar", res.getDrawable(R.drawable.ic_tab_calendar)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ProfileActivity.class);
spec = tabHost.newTabSpec("profile").setIndicator("Profile", res.getDrawable(R.drawable.ic_tab_profile)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
每个活动都有自己的内容布局视图,因此无需担心主布局。
您的主要XML布局将小而简单
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
我认为这就是你想要的。
答案 1 :(得分:0)
无法使标签与活动相对应。选项卡的目的是在一个视图中显示太多信息时将一个活动分解为多少个视图(不要与活动混淆)。然而,这是你如何设置TabHost: 首先从xml开始:
<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
-
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<AnalogClock
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="A semi-random button" />
</FrameLayout>
</LinearLayout>
</TabHost>
很抱歉这很难读,但是如果你把它复制到eclipse中你应该没问题。然后是Java:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
public class TabDemo extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TabHost tabs=(TabHost)findViewById(R.id.tabhost); //Id of tab host
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tag1");//make a new tab
spec.setContent(R.id.tab1); //What is in the tab (not an activity but rather a view)
spec.setIndicator("Clock"); //Name of tab
tabs.addTab(spec); //Add it
spec=tabs.newTabSpec("tag2"); //Same thing here
spec.setContent(R.id.tab2);
spec.setIndicator("Button");
tabs.addTab(spec);
}
}
这是相对简单的,我相信在android开发网站上有支持,当然只是在Google上搜索。这段代码是由Grant Allen从Beginning Android 4中复制出来的,本书更详细地解释了这个主题。祝你好运!