【问题标题】:Swipeable Tabs with toolbar instead of actionbar带有工具栏而不是操作栏的可滑动标签
【发布时间】:2015-11-08 14:54:17
【问题描述】:

我正在开发一个用于学校用途的应用程序。是否可以使用工具栏而不是使用操作栏创建可滑动的标签?因为我已经将我的样式主题更改为 NoActionbar,但我现在不知道如何添加 tablayout :( 在此先感谢 :)

【问题讨论】:

标签: android tabs android-toolbar


【解决方案1】:

尝试将 TabLayout 嵌入到 ToolBar 中,例如

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

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

然后将java代码中的所有内容设置为

Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar=getSupportActionBar();
actionBar.setTitle(null);

 String[] tabs={
        "Tab_1","Tab_2","Tab_3"
};

TabLayout tabLayout=(TabLayout) findViewById(R.id.tabLayout);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

for(String name:tabs){
    tabLayout.addTab(tabLayout.newTab().setText(name));
}

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多