如何在android中的选项卡屏幕中实现选项卡

时间:2015-10-26 03:51:17

标签: android android-fragments

我想在标签页面中实现标签,如下所示:

image

你能给我一个这种屏幕的例子吗?

2 个答案:

答案 0 :(得分:0)

有一个很好的Android Google网页@ SlidingTabsBasic和支持详情@ ViewPager class。几个月前,我有更多细节的相同答案,希望我能为你找到它。

我刚在SO @ Android - Google Play like tabs上找到了一个链接。天海的回答使用了我实施的示例项目。 Riccardo答案的示例项目(PagerSlidingTabStrip)也可能很好。我可以声称 SlidingTabsBasic 是一个像GUI项目这样的基本选项卡。

与此同时,这对你来说是一个好的开始吗?

答案 1 :(得分:0)

试试这个:

MainActivity

public class MainActivity extends TabActivity {

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Resources ressources = getResources();
    TabHost tabHost = getTabHost();

    // Android tab
    Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);
    TabSpec tabSpecAndroid = tabHost
            .newTabSpec("Android")
            .setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))
            .setContent(intentAndroid);

    // Apple tab
    Intent intentApple = new Intent().setClass(this, AppleActivity.class);
    TabSpec tabSpecApple = tabHost
            .newTabSpec("Apple")
            .setIndicator("", ressources.getDrawable(R.drawable.icon_apple_config))
            .setContent(intentApple);

    // Windows tab
    Intent intentWindows = new Intent().setClass(this, WindowsActivity.class);
    TabSpec tabSpecWindows = tabHost
            .newTabSpec("Windows")
            .setIndicator("", ressources.getDrawable(R.drawable.icon_windows_config))
            .setContent(intentWindows);

    // Blackberry tab
    Intent intentBerry = new Intent().setClass(this, BlackBerryActivity.class);
    TabSpec tabSpecBerry = tabHost
            .newTabSpec("Berry")
            .setIndicator("", ressources.getDrawable(R.drawable.icon_blackberry_config))
            .setContent(intentBerry);

    // add all tabs
    tabHost.addTab(tabSpecAndroid);
    tabHost.addTab(tabSpecApple);
    tabHost.addTab(tabSpecWindows);
    tabHost.addTab(tabSpecBerry);

    //set Windows tab as default (zero based)
    tabHost.setCurrentTab(2);
}

}

activity_main

<?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="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp">
    <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"
        android:padding="5dp" />
</LinearLayout>

AndroidActivity

public class AndroidActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Resources ressources = getResources();
    TabHost tabHost = getTabHost();

    // Android tab
    Intent intentAndroid = new Intent().setClass(this, AppleActivity.class);
    TabHost.TabSpec tabSpecAndroid = tabHost
            .newTabSpec("Android")
            .setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))
            .setContent(intentAndroid);

    // Apple tab
    Intent intentApple = new Intent().setClass(this, AppleActivity.class);
    TabHost.TabSpec tabSpecApple = tabHost
            .newTabSpec("Apple")
            .setIndicator("", ressources.getDrawable(R.drawable.icon_apple_config))
            .setContent(intentApple);

    // Windows tab
    Intent intentWindows = new Intent().setClass(this, WindowsActivity.class);
    TabHost.TabSpec tabSpecWindows = tabHost
            .newTabSpec("Windows")
            .setIndicator("", ressources.getDrawable(R.drawable.icon_windows_config))
            .setContent(intentWindows);

    // Blackberry tab
    Intent intentBerry = new Intent().setClass(this, BlackBerryActivity.class);
    TabHost.TabSpec tabSpecBerry = tabHost
            .newTabSpec("Berry")
            .setIndicator("", ressources.getDrawable(R.drawable.icon_blackberry_config))
            .setContent(intentBerry);

    // add all tabs
    tabHost.addTab(tabSpecAndroid);
    tabHost.addTab(tabSpecApple);
    tabHost.addTab(tabSpecWindows);
    tabHost.addTab(tabSpecBerry);

    //set Windows tab as default (zero based)
    tabHost.setCurrentTab(0);

 }

AppleActivity

public class AppleActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView textview = new TextView(this);
    textview.setText("This is Apple tab");
    setContentView(textview);
}

BlackBerryActivity

public class BlackBerryActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView textview = new TextView(this);
    textview.setText("This is BlackBerry tab");
    setContentView(textview);
}

WindowActivity

public class WindowsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView textview = new TextView(this);
    textview.setText("This is Windows mobile tab");
    setContentView(textview);
}