【问题标题】:requestFeature with action bar带有操作栏的 requestFeature
【发布时间】:2015-02-20 07:21:37
【问题描述】:

所以我正在查看 google 的操作栏 api 演示,他们有这个

// The Action Bar is a window feature. The feature must be requested
    // before setting a content view. Normally this is set automatically
    // by your Activity's theme in your manifest. The provided system
    // theme Theme.WithActionBar enables this for you. Use it as you would
    // use Theme.NoTitleBar. You can add an Action Bar to your own themes
    // by adding the element <item name="android:windowActionBar">true</item>
    // to your style definition.
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

但是当我尝试添加最后一行代码getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 时,我的应用程序在启动时就死机了。那么它到底有什么作用呢?

【问题讨论】:

  • 阅读您的日志猫并发布导致您的应用崩溃的异常的副本。
  • 很可能,您说得太晚了。 requestFeature() 需要在setContentView() 之前调用。
  • @CommonsWare 是的,这就是它不起作用的原因。谢谢。这很奇怪,因为它只在模拟器中产生错误,而不是语法错误。

标签: android android-actionbar


【解决方案1】:

秘密是调用的顺序。必须在不填充任何内容之前调用请求功能。我按这个顺序做,效果很好:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.your_activity_layout);

【讨论】:

    【解决方案2】:

    我知道这很晚了,你试过了吗

    requestWindowFeature(Window.FEATURE_ACTION_BAR)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多