Java 如何摆脱Android应用程序栏中的后退按钮

Java 如何摆脱Android应用程序栏中的后退按钮,java,android,android-fragments,android-actionbar,appbar,Java,Android,Android Fragments,Android Actionbar,Appbar,我有一个简单的活动,其中包含几个片段,用户可以在这些片段之间切换。只有在显示第一个片段时,应用程序栏中才会显示“后退”按钮 我已尝试通过将以下内容添加到“活动”中来删除代码中的“后退”按钮: @Override public boolean onCreateOptionsMenu(Menu menu) { if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabl

我有一个简单的活动,其中包含几个片段,用户可以在这些片段之间切换。只有在显示第一个片段时,应用程序栏中才会显示“后退”按钮

我已尝试通过将以下内容添加到“活动”中来删除代码中的“后退”按钮:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(false);
    }

    return super.onCreateOptionsMenu(menu);
}
但这并不能正常工作:一开始后退按钮实际上不见了(耶!)。但一旦我换到另一个片段,然后回到第一个片段,返回按钮又出现了

这个后退按钮是从哪里来的?
如何才能永久禁用它?

查看xml文件您是否可以在那里添加工具栏 试一试

setSupportActionBar()=null

建议您使用Android工具栏替换Actionbar。
1.转到Android清单文件并找到要使用的主题。
通常,默认值为android:theme=“@style/AppTheme”。您只需将第一行更改为Theme.AppCompat.Light.NoActionBar。
2.将工具栏控件写入布局文件。
3.在java文件中
绑定工具栏并调用setSupportActionBar()方法将当前菜单栏设置为绑定的工具栏对象。
Toolbar Toolbar=findviewbyd(R.id.Toolbar);
设置支持操作栏(工具栏);
如果要使用返回按钮,请添加返回的侦听器finish:
toolbar.setNavigationOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
onBackPressed();
}
});
You are advised to use the Android toolbar to replace the Actionbar.
1. Go to the Android Manifest file and find the theme to be used.
Generally, the default value is android:theme="@style/AppTheme". You only need to change the first line to Theme.AppCompat.Light.NoActionBar.
2. Write the toolbar control to the layout file.

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

3. In the java file
Bind the toolbar and invoke the setSupportActionBar() method to set the current menu bar to the bound toolbar object.
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
If you want to use the return button, add the returned listener finish:

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});