【问题标题】:Switch between tabs via swipe in android在android中通过滑动在标签之间切换
【发布时间】:2014-12-29 17:13:43
【问题描述】:

我正在使用选项卡和 TabHost 创建 android 应用程序,但用户无法通过滑动在选项卡之间切换。 我为这个应用程序使用 TabActivity。用户最常点击标签在标签或活动之间切换。

MainActivity.Java:

public class MainActivity extends TabActivity {

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

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

    //Home Intent
    Intent intentHome = new Intent().setClass(this, HomeActivity.class);
    TabSpec tabSpedHome = tabHost
            .newTabSpec("Home")
            .setIndicator("",ressources.getDrawable(R.drawable.icon_home_config))
            .setContent(intentHome);

    //Search Intent
    Intent intentSearch = new Intent().setClass(this, SearchActivity.class);
    TabSpec tabSpedSearch = tabHost
            .newTabSpec("Search")
            .setIndicator("",ressources.getDrawable(R.drawable.icon_search_config))
            .setContent(intentSearch);

    //Archive Intent
    Intent intentArchive = new Intent().setClass(this, ArchiveActivity.class);
    TabSpec tabSpedArchive = tabHost
            .newTabSpec("Archive")
            .setIndicator("",ressources.getDrawable(R.drawable.icon_archive_config))
            .setContent(intentArchive);

    //Download Intent
    Intent intentDownload = new Intent().setClass(this, DownloadActivity.class);
    TabSpec tabSpedDownload = tabHost
            .newTabSpec("Download")
            .setIndicator("",ressources.getDrawable(R.drawable.icon_download_config))
            .setContent(intentDownload);

    //Conctactus Intent
    Intent intentConctactus = new Intent().setClass(this, ContactusActivity.class);
    TabSpec tabSpedConctactus = tabHost
            .newTabSpec("Conctact us")
            .setIndicator("",ressources.getDrawable(R.drawable.icon_contactus_config))
            .setContent(intentConctactus);

    //AddAllTab
    tabHost.addTab(tabSpedHome);
    tabHost.addTab(tabSpedSearch);
    tabHost.addTab(tabSpedArchive);
    tabHost.addTab(tabSpedDownload);
    tabHost.addTab(tabSpedConctactus);

    //Set Default Tab
    tabHost.setCurrentTab(0);
}

}

还有activity_main.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="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>
</TabHost>

【问题讨论】:

    标签: android tabs android-tabhost swipe


    【解决方案1】:

    TabActivity 已被弃用 三年多。请使用其他选项卡解决方案。特别是如果您希望标签内容能够被滑动,请使用ViewPager 作为标签内容并使用标签指示符(例如,PagerTabStrip)作为标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      相关资源
      最近更新 更多