从 Fragment 中完全删除 Action Bar

Removing Action Bar completely from Fragment

我目前正在尝试更改应用程序的外观。

该应用程序使用了 3 个可以根据网络状态变化调用的片段。片段都设置为横向,这导致操作栏成为屏幕的很大一部分。我正在尝试找到一种方法来完全删除操作栏并允许布局扩展到整个屏幕。

我已经尝试过类似的东西:

getActionBar().hide();

主题变化

setSystemUiVisibility();

这些选项要么在屏幕上应有的位置创建一个大栏,要么只显示操作栏。

我认为这是我不理解的片段工作方式。

如果您使用的是 AppCompatActivity,那么请这样做。

((AppCompatActivity )getActivity()).getSupportActionBar();

如果您想从您的应用程序中完全删除 ActionBar (AppBar),请按照以下步骤操作:

  • Go to your AndroidManifest.xml
  • Edit this line: android:theme="@style/{theme}"> to android:theme="@style/Theme.AppCompat.Light.NoActionBar"> or anything that ends with NoActionBar.

如果您想从 Fragment 中删除 ActionBar,则在片段的 onCreateView() 中添加下一个代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // create ContextThemeWrapper from the original Activity Context with the custom theme
    final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme);

    // clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

    // inflate the layout using the cloned inflater, not default inflater
    return localInflater.inflate(R.layout.yourLayout, container, false);
}

或者,如果您想从 Activity 中删除 ActionBar,请将这一行代码添加到 Activity 的 onCreate() 方法中.

setTheme(R.style.{your style}

Remember

  • The {style} should end with NoActionBar
  • And most importantly, add the code before these two lines of code (add it to the top of the method):

    super.onCreate(savedInstanceState);
    setContentView(R.layout.{your layout});
    

希望这个回答对您有所帮助!