Android 如何在材质设计中创建后退按钮
我找不到有关在“材质设计”的操作栏中添加此按钮的教程 如何将此添加到棒棒糖的操作栏中?试试这个 在“创建时”中:Android 如何在材质设计中创建后退按钮,android,button,android-actionbar,Android,Button,Android Actionbar,我找不到有关在“材质设计”的操作栏中添加此按钮的教程 如何将此添加到棒棒糖的操作栏中?试试这个 在“创建时”中: getSupportActionBar().setDisplayHomeAsUpEnabled(true); 在活动类中(假设要关闭此活动) 这将使您简要了解如何实现material应用程序 如果将ActionBarActivity与AppCompat主题一起使用,请使用: getSupportActionBar().setDisplayHomeAsUpEnabled(true
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
在活动类中(假设要关闭此活动)
这将使您简要了解如何实现material应用程序
如果将ActionBarActivity
与AppCompat主题一起使用,请使用:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
此外,您可能需要以相同的方式调用setHomeButtonEnabled(true)
。
它将如下所示:
首先,您必须使用材料设计的主题,并且主题支持动作栏,如主题.AppCompat.Light
,主题.AppCompat.Light.darkaActionBar
。
第二次,调用ActionBar.setDisplayHomeAsUpEnabled(true)代码>或工具栏。setDisplayHomeAsUpEnabled(true)
,然后返回图标将显示。在onCreate中添加这些行
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
对于反向导航,您必须在AndroidMnifest.xml
<activity
android:name=".CurrentActivity"
android:label="@string/app_name"
android:parentActivityName=".BackActivity">
</activity>
getSupportActionBar().setDisplayHomeAsUpEnabled(true)
可能会产生null指针异常,onCreate()
应该是这样的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
请发布一些示例代码来演示您的尝试:)问题是,我无法在含有棒棒糖的action bar中找到任何相关内容!您正在使用支持库吗?你的活动扩展了什么?@Harry-我有两个活动。我是初学者。我只需要教程……小心:标题有误导性,这不是“后退”按钮,而是向上按钮。使用正确的术语。“后退”按钮是每个安卓设备都有的按钮。你有关于这方面的例子吗?我尝试了一下,但出现了错误:不幸的是,程序停止了
给我。tnxcan你可以从logcatmasterphp
粘贴tracelog,它试图实现材料设计。如何使用它getActionBar()
。它必须是getSupportActionBar()
@Tomershemesh如果不需要导航抽屉,请不要使用它。只需在主要活动中使用上面的代码行即可。谢谢,已接受,现在可以使用:getSupportActionBar()。setDisplayHomeAsUpEnabled(true)代码>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}