Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

java - ActionBar in PreferenceActivity

Example: Android Market

In my application I am using the new Action Bar Compatibility sample from Google (located at <sdk>/samples/android-<version>/ActionBarCompat) which works great. The only problem I have is applying this to my PreferenceActivity in order to get a screen like the settings in the Android Market (see picture).

To fill the ActionBar with icons, each Activity must extend the ActionBarActivity class. The problem is that my Activity already extends PreferenceActivity and in Java classes can not extend more than one class.

There must be a way to get the ActionBar together with a PreferenceScreen. I would be glad if anybody could provide a solution for this common issue.

P.S.: A solution like in How to add a button to PreferenceScreen does not fit because the ActionBar is actually the title bar and so this is more a Java than a layout thing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Edit: My answer below is rather hacky and it seems like it is now outdated (for pre Android 3.0) Have a look at the other answers for less hacky and more current solutions ~pyko 2014-09-01


I managed to get it working - not sure if this is the nicest/cleanest solution, but it works.

Had to make the following changes:

  1. Make a copy of ActionBarActivity and have the new class extend PreferenceActivity

    public abstract class ActionBarPreferenceActivity extends PreferenceActivity {
        // contents exactly the same as 'ActionBarActivity'
    }
    
  2. Modify onCreate() in ActionBarHelperBase.java slightly - make a special case for PreferenceActivity classes

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // If the activity is a PreferenceActivity, don't make the request
        if (!(mActivity instanceof PreferenceActivity)) {
            mActivity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        }
    
  3. Have your PreferenceActivity extend this class and add request for FEATURE_CUSTOM_TITLE before you call super.onCreate()

    public class MyPreferenceActivity extends ActionBarPreferenceActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // add this line
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            // etc etc
        }
    
        // etc etc
    }
    

As far as I can tell, changes 2 and 3 are needed because for PreferenceActivity:

"As soon as you call super.onCreate(), the ViewGroup will be set up and so, you are not allowed to change the Window's parameters." (see Oliver's comment to the answer)

I guess the order of how components in PreferenceActivity activities are created is different to plain Activity activities .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...