2016-10-03 137 views
0

我使用ViewPager創建了標籤。但標籤的標題出現在屏幕的中心。我想將標題保留在屏幕的頂部。請檢查我的以下XML代碼和Java文件。請讓我知道如何解決這個問題。無法將標籤標題設置爲頂部

這是MainActivity xml文件:

<?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; 
    } 
} 

回答

0

TabLayout是出現在屏幕中央的原因是因爲你有layout_height設置爲match_parentTabLayout中的項目的默認gravitycenter_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); 
+0

我剛換到Android:layout_height = 「WRAP_CONTENT」 在TabLayout。謝謝你 –