Java 使用带有视图而不是活动的 Android 选项卡的示例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2970844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Example of using Android tabs with Views instead of Activities?
提问by David
The Android Developers TabWidget tutorial says the following:
Android Developers TabWidget 教程如下:
"You can implement your tab content in one of two ways: use the tabs to swap Views within the same Activity, or use the tabs to change between entirely separate activities."
“您可以通过以下两种方式之一实现标签内容:使用标签在同一活动中交换视图,或使用标签在完全独立的活动之间进行切换。”
The tutorial goes on to demonstrate how you can use tabs with separate Activities. I have been unable to find an example of using tabs with different Views within the same Activity. I would rather not re-invent this particular wheel, so I am hoping someone here knows how this is done and can clue me in. Thanks!
本教程继续演示如何使用带有单独活动的选项卡。我一直无法找到在同一活动中使用具有不同视图的选项卡的示例。我宁愿不重新发明这个特定的轮子,所以我希望这里有人知道这是如何完成的并且可以给我提供线索。谢谢!
回答by ninjasense
I think in the .setContent method of each tab you pass in the view you wish to use:
我认为在您希望使用的视图中传递的每个选项卡的 .setContent 方法中:
TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
spec1.setContent(R.id.AnalogClock01);
spec1.setIndicator("Analog Clock");
Here's an example I found awhile back:
这是我不久前发现的一个例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabHost android:id="@+id/TabHost01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<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="wrap_content" android:layout_height="wrap_content" android:paddingTop="65px">
<AnalogClock android:id="@+id/AnalogClock01" android:layout_width="wrap_content" android:layout_height="wrap_content"></AnalogClock>
<DigitalClock android:text="DigitalClock01" android:id="@+id/DigitalClock01" android:layout_width="wrap_content" android:layout_height="wrap_content"></DigitalClock>
</FrameLayout>
</TabHost>
</LinearLayout>
And the Java code for this example is as follows:
这个例子的Java代码如下:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
public class tabexample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
tabs.setup();
TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
spec1.setContent(R.id.AnalogClock01);
spec1.setIndicator("Analog Clock");
tabs.addTab(spec1);
TabHost.TabSpec spec2 = tabs.newTabSpec("tag2");
spec2.setContent(R.id.DigitalClock01);
spec2.setIndicator("Digital Clock");
tabs.addTab(spec2);
}
}
回答by Islam A. Hassan
I used this one and it was fine for me http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android
我使用了这个,对我来说很好 http://www.codeproject.com/Articles/107693/Tabbed-Applications-in-Android