本文介绍了在首选项活动中添加带有后退按钮的操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的偏好活动:

package com.example.hms.test;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class PrefsActivity extends PreferenceActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
    }

}

在这里,我想显示一个带有名称设置和返回主页的后退按钮的操作栏

here I want to show an actionbar with name settings and a back button to home

推荐答案

您应该做几件事:

  1. 将以下内容添加到PreferenceActivity的onCreate中:

  1. Add the following to your onCreate of PreferenceActivity:

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

  • 覆盖在PreferenceActivity中选择的onOptionsItem:

  • Override onOptionsItemSelected in PreferenceActivity:

    @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId())
         {
             case android.R.id.home:
                 NavUtils.navigateUpFromSameTask(this);
                 return true;
         }
         return super.onOptionsItemSelected(item);
     }
    

  • 为您的PreferenceActivity更改清单中的<activity>标记,如下所示:

  • change the <activity> tag in manifest for your PreferenceActivity to look something like this:

    <activity
      android:name=".PrefsActivity"
      android:label="@string/title_activity_settings"
      android:parentActivityName=".MainActivity">
      <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.android.MainActivity" />
    </activity>
    

  • 最后将android:launchMode ="singleTop"放入清单中的MainActivity <activity>标记中:

  • Finally put android:launchMode="singleTop" in your MainActivity <activity> tag in manifest:

    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:launchMode="singleTop"
      android:theme="@style/AppTheme.NoActionBar">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    

  • 这篇关于在首选项活动中添加带有后退按钮的操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-29 19:45