【问题标题】:Toolbar title not showing in tabbed activity选项卡式活动中未显示工具栏标题
【发布时间】:2016-08-19 13:27:19
【问题描述】:

使用 TabsLayout 实现了一个选项卡式活动,但现在该活动的标题没有显示在工具栏上,尝试了我在互联网上找到的各种修复,但似乎都没有。

活动的xml:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:title="title"
    android:paddingTop="@dimen/appbar_padding_top"
    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:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:tabMode="scrollable">

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

    </android.support.v7.widget.Toolbar>

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

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

创建时: protected void onCreate(Bundle savedInstanceState) { super.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);

【问题讨论】:

    标签: java android


    【解决方案1】:

    你的 tabLayout 不应该是你的工具栏的子元素,它应该在它下面,并且用户 ?attr/actionBarSize 女巫会给你准确的操作栏大小,无论设备 dpi 是什么

    <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:title="title"
    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:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    </android.support.v7.widget.Toolbar>
     <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:marginTop="?attr/actionBarSize"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:tabMode="scrollable">
        </android.support.design.widget.TabLayout>
       </android.support.design.widget.AppBarLayout>
       <android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多