【问题标题】:What is the alternative to getActionBar()?getActionBar() 的替代方法是什么?
【发布时间】:2015-06-15 18:44:26
【问题描述】:

当我尝试使用getActionBar() 时,它说它已被弃用,这意味着我们不允许继续使用getActionBar()

在当前情况下,学习如何创建 Tabs 的最佳方法是什么?

【问题讨论】:

  • 使用新的Toolbar

标签: android


【解决方案1】:

如果你使用appcompat,正确的方法是getSupportActionBar()

【讨论】:

    【解决方案2】:

    你应该创建这个的原因,因为 Fragment Tabs 使用 ActionBar 现在已弃用。这是创建操作栏的新方法 材料设计。要使用它,您需要在 build.gradle 文件中添加依赖项。

    compile 'com.android.support:appcompat-v7:21.0.0'
    

    创建工具栏app_bar.xml 文件,你可以给你想要的名字。

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
    

    确保您在styles.xml 中选择了主题

    "Theme.AppCompat.NoActionBar"
    

    之后,你需要在你的 mainActivity.xml 布局文件中初始化这个工具栏,包括这个,

     <include
            android:id="@+id/app_toolbar"
            layout="@layout/app_toolbar"/>
    

    然后在 MainActivity 中,您需要创建 Toolbar 工具栏的实例。然后,在 setContentView() 之后 用您的

    添加以下内容
     toolbar = (Toolbar) findViewById(R.id.app_toolbar);
        setSupportActionBar(toolbar);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-13
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多