java工具栏标题未显示在选项卡式活动中
使用TabsLayout实现了一个选项卡式活动,但现在该活动的标题没有显示在工具栏上,我尝试了在internet上找到的各种修复方法,但似乎都不起作用
活动的xml:
<安卓.support.design.widget.AppBarLayout
安卓:id="@+id/appbar"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
app:title="title"
安卓:paddingTop="@dimen/appbar_padding_top"
安卓:theme="@style/AppTheme.AppBarOverlay">
<安卓.support.v7.widget.Toolbar
安卓:id="@+id/toolbar"
安卓:layout_width="match_parent"
安卓:layout_height="?attr/actionBarSize"
安卓:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
<安卓.support.design.widget.TabLayout
安卓:id="@+id/tabs"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:paddingTop="@dimen/appbar_padding_top"
安卓:theme="@style/AppTheme.AppBarOverlay"
app:tabMode="scrollable">
</安卓.support.design.widget.TabLayout>
</安卓.support.v7.widget.Toolbar>
</安卓.support.design.widget.AppBarLayout>
<安卓.support.v4.view.ViewPager
安卓:id="@+id/container"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
OnCreate: 创建时受保护的void(Bundle savedInstanceState){ 超级的onCreate(savedInstanceState); setContentView(R.layout.activity_ordenes_de_viaje_tabbed)
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
Intent intent = getIntent();
item = intent.getIntExtra("item", 0);
mViewPager.setCurrentItem(item);
Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mActionBarToolbar);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My title");
getSupportActionBar().setDisplayShowTitleEnabled(true);
# 1 楼答案
你的tabLayout不应该是你工具栏的子工具栏,它应该在它下面,用户呢?attr/actionBarSize witch将为您提供准确的操作栏大小,无论设备dpi如何