gpt4 book ai didi

android - ActionBarCompat : Hide ActionBar before activity is created (bug?)

转载 作者:IT老高 更新时间:2023-10-28 23:31:55 29 4
gpt4 key购买 nike

所以我在使用 ActionBarSherlock 并决定切换到新的 ActionBarCompat。使用 ABS,可以使用本文中描述的方式隐藏 ActionBar: How to hide action bar before activity is created, and then show it again?

但是,使用 ActionBarCompat,应用程序会在 API14 上崩溃,因为当您将 android:windowActionBar 设置为 false 时,getSupportActionBar() 方法会返回null,即使您已将 getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 声明到 onCreate() 方法中。

有趣的是,如果您改为调用 getActionBar(),您会得到对象并且一切正常。

那么,这是一个错误还是我错过了什么?欢迎任何想法!


styles.xml 文件:

<style name="Theme.MyApp" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowActionBar">false</item>
<item name="android:windowTitleSize">0dp</item>
</style>

MyActivity.java 文件:

...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the action bar feature. This feature is disabled by default into the theme
// for specific reasons.
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
...
// By default the action bar is hidden.
getSupportActionBar().hide();
}

最佳答案

我遇到了同样的问题,在我看来,找到了这种奇怪行为的原因。我查看了 support library 的来源得到了这个:

Appcompat 在 ActionBarActivityDelegate 中创建新操作栏之前检查 mHasActionBar 变量的值

final ActionBar getSupportActionBar() {
// The Action Bar should be lazily created as mHasActionBar or mOverlayActionBar
// could change after onCreate
if (mHasActionBar || mOverlayActionBar) {
if (mActionBar == null) {
mActionBar = createSupportActionBar();
...

我们可以通过调用 supportRequestWindowFeature(int featureId) 来更改它的值,它由 ActionBarActivity 委托(delegate)给 ActionBarActivityDelegate .

有基础委托(delegate)类ActionBarDelegateBase及其后代ActionBarDelegateHCActionBarActivityDelegateICSActionBarActivityJB,其中之一是根据运行android的版本选择。并且方法 supportRequestWindowFeature 实际上几乎在所有这些中都可以正常工作,但是它在 ActionBarActivityDelegateICS 中被覆盖

@Override
public boolean supportRequestWindowFeature(int featureId) {
return mActivity.requestWindowFeature(featureId);
}

所以它对变量mHasActionBar没有影响,这就是getSupportActionBar()返回null的原因。

我们快到了。我想到了两种不同的解决方案。

第一种方式

  1. git 导入 appcompat 的源项目

  2. ActionBarActivityDelegateICS.java 中的重写方法更改为类似这样的方法

    @Override
    public boolean supportRequestWindowFeature(int featureId) {
    boolean result = mActivity.requestWindowFeature(featureId);
    if (result) {
    switch (featureId) {
    case WindowCompat.FEATURE_ACTION_BAR:
    mHasActionBar = true;
    case WindowCompat.FEATURE_ACTION_BAR_OVERLAY:
    mOverlayActionBar = true;
    }
    }
    return result;
    }
  3. 将此行放在 Activity 的 onCreate 方法中 getSupportActionBar()

    之前
    supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);

第二种方式

  1. 从android SDK导入appcompat项目(src目录为空)

  2. 将此方法添加到您的 Activity 中

    private void requestFeature() {
    try {
    Field fieldImpl = ActionBarActivity.class.getDeclaredField("mImpl");
    fieldImpl.setAccessible(true);
    Object impl = fieldImpl.get(this);

    Class<?> cls = Class.forName("android.support.v7.app.ActionBarActivityDelegate");

    Field fieldHasActionBar = cls.getDeclaredField("mHasActionBar");
    fieldHasActionBar.setAccessible(true);
    fieldHasActionBar.setBoolean(impl, true);

    } catch (NoSuchFieldException e) {
    Log.e(LOG_TAG, e.getLocalizedMessage(), e);
    } catch (IllegalAccessException e) {
    Log.e(LOG_TAG, e.getLocalizedMessage(), e);
    } catch (IllegalArgumentException e) {
    Log.e(LOG_TAG, e.getLocalizedMessage(), e);
    } catch (ClassNotFoundException e) {
    Log.e(LOG_TAG, e.getLocalizedMessage(), e);
    }
    }
  3. 在你的 Activity 的 onCreate 方法中调用 requestFeature() 像这样

    if (Build.VERSION.SDK_INT >= 11) {
    requestFeature();
    }
    supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);

我使用了第二种方式。就是这样。

关于android - ActionBarCompat : Hide ActionBar before activity is created (bug?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18526144/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com