简体   繁体   中英

Custom Actionbar Sherlock PreferenceActivity ListView

I use SherlockPreferenceActivity for Preference activity from a xml file. Code below:

public class Setting extends SherlockPreferenceActivity
{
protected void onCreate(Bundle paramBundle)
{
  super.onCreate(paramBundle);
  addPreferencesFromResource(R.xml.setting);
}

Everything work fine but i need the margin left and right is 0dip (the pic below) 在此处输入图片说明

I had try to find in ABS source code but failed to custom it. Anyone has do it before?

Update: I upload more detail image 在此处输入图片说明

setting.xml content:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="@string/system_setting" >
        <CheckBoxPreference
            android:defaultValue="true"
            android:key="mode_on_off"
            android:summaryOff="Sub Setting 1"
            android:summaryOn="Sub Setting 1 On"
            android:title="Setting 1" />
        <CheckBoxPreference
            android:defaultValue="true"
            android:key="filter_on_off"
            android:summaryOff="Sub Setting 2"
            android:summaryOn="Sub Setting 2 On"
            android:title="Setting 2" />
        <CheckBoxPreference
            android:defaultValue="false"
            android:key="screen"
            android:summaryOff="Sub Setting 3"
            android:summaryOn="Sub Setting 3 on"
            android:title="Setting 2" />

    </PreferenceCategory>
</PreferenceScreen>

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 .

use this way:

在此处输入图片说明

in Activity:

import android.os.Bundle;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockPreferenceActivity;

public class MainActivity extends SherlockPreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.custom_infowindow);
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setCustomView(R.layout.custom_infowindow);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);

        addPreferencesFromResource(R.xml.test);
    }

}

custom_infowindow.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Right Now Event"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FF0000" />

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@color/_blue" />

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM