Java 无法识别Android工具栏后退按钮

Java 无法识别Android工具栏后退按钮,java,android,xml,android-toolbar,Java,Android,Xml,Android Toolbar,我正在为android应用程序的设置屏幕工作 设置屏幕包含两个“子屏幕”,都有工具栏和后退按钮 屏幕1上的后退按钮不起任何作用 屏幕1上的后退按钮工作正常,并将我返回到主设置屏幕“硬”后退按钮适用于两个屏幕 屏幕1首选项屏幕: 屏幕1 xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

我正在为android应用程序的设置屏幕工作

设置屏幕包含两个“子屏幕”,都有工具栏和后退按钮

屏幕1上的后退按钮不起任何作用 屏幕1上的后退按钮工作正常,并将我返回到主设置屏幕“硬”后退按钮适用于两个屏幕

屏幕1首选项屏幕:

屏幕1 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_preferences"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.clear.pocketcross.Preferences">

    <Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/colorPrimary"
        android:popupTheme="@android:style/ThemeOverlay.Material.Light"
        android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <Switch
            android:id="@+id/counterscroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:defaultValue="true"
            android:key="counter_scroll"
            android:shadowColor="@color/gradient_mid"
            android:shadowDx="5"
            android:shadowDy="5"
            android:text="@string/scrollCounters"
            android:thumb="@drawable/pocketcross_btn_radio_on_holo_light" />

        <Switch
            android:id="@+id/navswipe"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:defaultValue="true"
            android:key="nav_swipe"
            android:shadowColor="@color/gradient_mid"
            android:shadowDx="5"
            android:shadowDy="5"
            android:text="@string/swipeTitle"
            android:thumb="@drawable/pocketcross_btn_radio_on_holo_light" />
    </LinearLayout>

</RelativeLayout>
屏幕2袖珍交叉参数屏幕:

屏幕2的XML:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parametersTable"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="*"
    android:stretchColumns="*"
    android:background="@color/background"
    android:clickable="false">

    <Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/colorPrimary"
        android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar"
        android:popupTheme="@android:style/ThemeOverlay.Material.Light" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/parametersList"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_below="@+id/toolbar" />

</RelativeLayout>
我很难理解的是,为什么我的应用程序中每个屏幕上的后退按钮都能完美工作,除了这个


我猜这将是显而易见的,但我不知道是什么。

您可以检查以下内容

1您的活动扩展AppCompatActivity 二,

3重写以下方法

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()== android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

在manifest.xml中添加以下行:android:parentActivityName=。要返回的活动的名称

就这样

<activity
            android:name=".(CurrentActivity)"
            android:parentActivityName=".(Activity to be returned)"/>

问题似乎出在xml文件上。您已将父布局指定为clickable false,因此无法获得单击调用。尝试删除它。

您是否尝试从RelativeLayout中删除android:clickable=false?您还可以删除android:shrinkColumns和android:stretchColumns,因为它们是TableLayoutproperties@MatPag“android:clickable=false”仅出现在屏幕2上。屏幕2工作正常。屏幕1不起作用。这只在使用com.android.support.appcompat-v7的情况下起作用。这个答案可能很好,但有一件事更简单:使用getSupportActionBar.setNavigationOnClickListenerView.OnClickListener;这可能是一个很好的评论,但这并不能回答问题。这里的问题是为什么返回按钮不起作用,而不是如何返回到另一个活动谢谢。我觉得自己像个白痴。我从没想过要检查舱单!!!两个屏幕之间的区别是屏幕2在清单中设置了父屏幕,而屏幕1没有!我很高兴我能帮上忙:
@Override
public boolean onOptionsItemSelected(MenuItem item) { 
        switch (item.getItemId()) {
        case android.R.id.home: 
            onBackPressed();
            return true;
        }

    return super.onOptionsItemSelected(item);
}
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()== android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
<activity
            android:name=".(CurrentActivity)"
            android:parentActivityName=".(Activity to be returned)"/>