无法将标签标题设置为顶部

时间:2016-10-03 13:23:31

标签: android android-viewpager android-tablayout

我使用ViewPager创建了标签。但是标签的标题出现在屏幕的中央。我想将标题保留在屏幕顶部。请检查我的以下xml代码和java文件。请告诉我如何解决这个问题。

这是MainActivity

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    tools:context="com.example.bright.myapplication.MainActivity">

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:layout_centerHorizontal="true"
            android:id="@+id/tablayout"
            android:layout_below="@+id/toolbar">

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Left" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Center" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Right" />

        </android.support.design.widget.TabLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

            />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/toolbar" />
    </RelativeLayout>

以下是第一个片段的xml文件:

fragment_blank_fragment1.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.bright.myapplication.BlankFragment1">

        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello_blank_fragment 1" />

    </FrameLayout>

以下是第二个片段的xml文件:

fragment_blank_fragment2.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.bright.myapplication.BlankFragment2">

        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello_blank_fragment 2" />

    </FrameLayout>

and one more xml file for a fragment

以下是Activity文件:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    TabLayout tabLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = (TabLayout)findViewById(R.id.tablayout);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(),3);
        ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);
        viewPager.setAdapter(myAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    }
}

以下是我通过ViewPager将片段返回主活动的地方:

MyAdapter.java

public class MyAdapter extends FragmentStatePagerAdapter {
    int tabcount;
    public MyAdapter(FragmentManager fm, int count) {
        super(fm);
        tabcount = count;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                BlankFragment1 fragment1 = new BlankFragment1();
                return fragment1;
            case 1:
                BlankFragment2 fragment2 = new BlankFragment2();
                return fragment2;
            case 2:
                BlankFragment3 fragment3 = new BlankFragment3();
                return fragment3;
            default:
                return null;
        }

    }

    @Override
    public int getCount() {
        return tabcount;
    }
}

1 个答案:

答案 0 :(得分:0)

TabLayout出现在屏幕中央的原因是因为layout_height设置为match_parentgravity中的项目的默认TabLayoutcenter_vertical

我假设您希望Material Design Guidelines符合标签的标准外观,在这种情况下,MainActivity的布局文件应该更像这样:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="wrap_content">

    <android.support.design.widget.AppBarLayout android:id="@+id/app_bar"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay/>

        <android.support.design.widget.TabLayout android:id="@+id/tab_layout"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            app:tabIndicatorColor="?attr/colorAccent">

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="@string/tab_left"/>

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="@string/tab_center"/>

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="@string/tab_right"/>

        </android.support.design.widget.TabLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.ViewPager android:id="@+id/pager"
        android:layout_width="match_parent" android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>

此外,还有一种使用ViewPager设置标签的便捷方法,而不是调用viewPager.setOnPageChangeListener()

tabLayout.setupWithViewPager(viewPager);
相关问题