我尝试制作一个扩展AppCompatPreferenceActivity的活动,并在操作栏中实现一个向上按钮。视觉上一切看起来都很好,但向上按钮不响应触摸事件。以下是我的java和xml代码:
的 PrefrencesDisplayActivity.java
package com.example.matthew.testsettings;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import android.widget.RelativeLayout;
import java.util.List;
public class PreferencesDisplayActivity extends AppCompatPreferenceActivity {
CoordinatorLayout activityRoot;
AppBarLayout appBarLayout;
Toolbar toolbar;
RelativeLayout contentRoot;
ListView preferencesList;
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preferences, target);
initialiseUI();
}
private void initialiseUI() {
// Inflate activity layout from xml
setContentView(R.layout.activity_preferences_display);
// Get handles to UI elements
activityRoot = (CoordinatorLayout) findViewById(R.id.prefDisplay_activity_root);
appBarLayout = (AppBarLayout) findViewById(R.id.prefdisplay_activity_appBarLayout);
toolbar = (Toolbar) findViewById(R.id.prefDisplay_activity_toolbar);
contentRoot = (RelativeLayout) findViewById(R.id.prefDisplay_content_root);
preferencesList = (ListView) findViewById(android.R.id.list);
// Create and configure action bar
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
activity_prefrences_display.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:theme="@style/AppTheme.NoActionBar"
android:id="@+id/prefDisplay_activity_root"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.matthew.testsettings.PreferencesDisplayActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/prefdisplay_activity_appBarLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/prefDisplay_activity_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_preferences_display"/>
</android.support.design.widget.CoordinatorLayout>
content_preferences_display.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/prefDisplay_content_root"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_preferences_display"
tools:context="com.example.matthew.testsettings.PreferencesDisplayActivity">
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.example.matthew.testsettings"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".PreferencesDisplayActivity"
android:label="@string/title_activity_preferences_display"
android:parentActivityName="com.example.matthew.testsettings.MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.matthew.testsettings.MainActivity"/>
</activity>
</application>
</manifest>
当我打开应用程序并导航到首选项活动时,屏幕看起来应该如此,但按下向上按钮不会执行任何操作。任何人都可以看到我的错误吗?
答案 0 :(得分:9)
按下后退按钮时,您也可以使用finish()
:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:0)
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
并覆盖onBackpressed
@Override
public void onBackPressed() {
super.onBackPressed();
}