2017-04-18 8 views
1

Good Day!私は現在アンドロイドアプリを作成中です。私の最初の問題は、私のTabLayoutに問題があります。ツールバーでTabLayoutを使用するには?

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Create the adapter that will return a fragment for each section 
     mPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) { 
      private final Fragment[] mFragments = new Fragment[] { 
        new MyPostsFragment(), 
        new MyPostsFragment(), 
        new MyPostsFragment(), 
      }; 
      private final String[] mFragmentNames = new String[] { 
        getString(R.string.heading_recent), 
        getString(R.string.heading_recent), 
        getString(R.string.heading_recent) 
      }; 
      @Override 
      public Fragment getItem(int position) { 
       return mFragments[position]; 
      } 
      @Override 
      public int getCount() { 
       return mFragments.length; 
      } 
      @Override 
      public CharSequence getPageTitle(int position) { 
       return mFragmentNames[position]; 
      } 
     }; 
     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mPagerAdapter); 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(mViewPager); 

     // Button launches NewPostActivity 
     findViewById(R.id.fab_new_post).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       startActivity(new Intent(MainActivity.this, NewReportActivity.class)); 
      } 
     }); 
    } 

そして、これはレイアウトです::

あなたが単に非表示にすることができます
<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".activities.MainActivity"> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@id/tabs" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab_new_post" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:src="@drawable/ic_add_new_report" 
     android:layout_margin="16dp"/> 

</RelativeLayout> 

enter image description here

答えて

0

あなたがActionBarを使用しないようにシステムを言っている、これを追加することでトリック

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 

を行います。

1

私はこれが私のMainActivityある

...ツールバーのtablayoutを表示したいですToolbarあなたが望むものを達成する。動的

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 

またはStyles通過:

android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" 
1

これを試してみてください、私は以下のコードを使用して、ツールバー内のタブを追加することができています。 parent="Theme.AppCompat.Light.NoActionBar"にあなたのテーマを拡張する

<android.support.design.widget.AppBarLayout 
    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/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabTextColor="@android:color/white" 
     app:tabSelectedTextColor="@color/aqua_marine" 
     app:tabIndicatorColor="@color/aqua_marine" 
     app:tabMode="fixed" 
     app:tabGravity="fill"> 
    </android.support.design.widget.TabLayout> 
    </android.support.v7.widget.Toolbar> 
</android.support.design.widget.AppBarLayout> 

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

<include layout="@layout/content_home" /> 

関連する問題