用选项卡滑动的水平视图

时间:2014-11-23 09:04:53

标签: android view tabs android-viewpager navigation-drawer

我和我的一位老师打赌,我需要制作一个访问学校网站的应用程序。我添加了一个TabBar,它有3个WebView s,如下所示: enter image description here

现在我希望能够在这些标签之间滑动。我尝试添加HorizontalScrollView但没有成功,然后我找到了关于实现ViewPager的内容,我认为这是解决方案,但我无法使其正常工作。

activity_main.xml:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TabHost
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tabHost"
        android:layout_gravity="start|top">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <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">

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">

                        <WebView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/webView" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">
                    <WebView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/webView2"
                        />

                </LinearLayout>
                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical">

                    <WebView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/webView3" />

                    </LinearLayout>


            </FrameLayout>
        </LinearLayout>
    </TabHost>
</FrameLayout>

<fragment
    android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.smash.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

和MainActivity.java中的一段:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
        tabHost.setup();

        mWebView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://schoolwebsite/");
        mWebView.setWebViewClient(new MyAppWebViewClient());

        TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("Home");
        tabSpec1.setContent(R.id.tab1);
        tabSpec1.setIndicator("Home");
        tabHost.addTab(tabSpec1);

        mWebView2 = (WebView) findViewById(R.id.webView2);
        webSettings.setJavaScriptEnabled(true);
        mWebView2.loadUrl("http://schoolwebsite");
        mWebView2.setWebViewClient(new MyAppWebViewClient());

        TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("Teachers");
        tabSpec2.setContent(R.id.tab2);
        tabSpec2.setIndicator("Teachers");
        tabHost.addTab(tabSpec2);

        mWebView3 = (WebView) findViewById(R.id.webView3);
        webSettings.setJavaScriptEnabled(true);
        mWebView3.loadUrl("http:/schoolwebsite");
        mWebView3.setWebViewClient(new MyAppWebViewClient());

        TabHost.TabSpec tabSpec3 = tabHost.newTabSpec("Students");
        tabSpec3.setContent(R.id.tab3);
        tabSpec3.setIndicator("Students");
        tabHost.addTab(tabSpec3);
        // Set up the drawer.

 mNavigationDrawerFragment.setUp( R.id.navigation_drawer,(DrawerLayout) findViewById(R.id.drawer_layout));

    }

Ow,几乎忘了提到我使用Android Studio提供的内置导航抽屉活动创建了我的应用程序。

抱歉我的英语不好。

0 个答案:

没有答案