gpt4 book ai didi

android - AppCompat PreferenceActivity 向上按钮不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:32 26 4
gpt4 key购买 nike

我正在尝试创建一个扩展 AppCompatPreferenceActivity 并在操作栏中实现向上按钮的 Activity 。视觉上一切看起来都很好,但向上按钮不响应触摸事件。以下是我的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>

当我打开应用程序并导航到首选项 Activity 时,屏幕看起来应该是正常的,但按下向上按钮没有任何反应。谁能看出我的错误?

最佳答案

您也可以在按下后退按钮时只使用 finish():

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

关于android - AppCompat PreferenceActivity 向上按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34410538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com