android - 如何将后退按钮放在 ActionBar 的左侧

标签 android xml android-layout android-activity

我正在构建并完成一个 Android 应用程序,这是我需要修复它的主要事情之一。

所以,我有一个样式如下的操作栏:

<resources>
    <style name="MyCustomTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
    </style>

    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#F6E6E7</item>
        <item name="android:textSize">10sp</item>
    </style>

    <style name="CustomTabWidget">
        <item name="android:textSize">15sp</item>
    </style>


</resources>

这是我放置按钮的布局

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->

    <item android:id="@+id/voltar"
        android:title="Voltar"
        android:icon="@drawable/abc_ic_ab_back_holo_light"
        android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->

</menu>

这个标题为 Voltar 的按钮位于操作栏的右侧。我应该怎么做才能将它放在左侧?

感谢您阅读本文

最佳答案

你需要做两件事。首先在您的 Activity 的 onCreate 中声明它

getActionBar().setDisplayHomeAsUpEnabled(true);

然后实现 onOptionsItemSelected 以将 home 处理为向上按钮点击事件

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home ) {
        finish();
        return true;
    }
    // other menu select events may be present here

    return super.onOptionsItemSelected(item);
}

关于android - 如何将后退按钮放在 ActionBar 的左侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27193388/

相关文章:

android - 旋转时保留 Fragment 对象

布局中定义的 Android EditText onClick 监听器因模糊异常而失败

java - 从 View android中删除所有监听器

java - android中的NullPointerException,可能是数据库问题

java - 本地类定义 : why does this work

android - 谷歌地图安卓 : snap polyline to street

带有两个图像和文本的 Android 按钮

Java - 使程序从文件加载选项的最佳方法是什么?

xml - 在 SVG 中对齐文本

android - 如何使用 TextView 填充 android 中 View 的剩余空间?