切换到PreferenceFragmentCompat后没有分隔符

时间:2018-12-30 20:12:00

标签: android android-studio

我无法在首选项之间添加分隔线,也无法在PreferenceFragmentCompat中设置填充 我试图从PreferenceFragment切换到PreferenceFragmentCompat几乎一切正常,但是我无法在首选项之间添加分隔线,也无法设置填充

我的设置和MainPreferenceFragment .java文件:

import android.content.*;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.*;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import com.mongodb.stitch.android.core.Stitch;
import com.mongodb.stitch.android.core.StitchAppClient;

public class SettingsActivity extends AppCompatActivity implements SharedPreferences
        .OnSharedPreferenceChangeListener
{
    private SharedPreferences sharedPrefs;
    private static StitchAppClient client = Stitch.getDefaultAppClient();
    private static int clickCounter = 0;
    private static final String LOG_TAG = SettingsActivity.class.getSimpleName();


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);

        // Get ActionBar and display LOGO {{
        ActionBar actionBar = getSupportActionBar();
        try {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);

        } catch (Exception e) {
            Log.e(LOG_TAG,"Error " + e.getMessage());
        }
    }


    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
        sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        // register preference change listener
        sharedPrefs.registerOnSharedPreferenceChangeListener(this);
    }

    public static class MainPreferenceFragment extends PreferenceFragmentCompat implements Preference
            .OnPreferenceChangeListener
    {

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.settings_main);

            //if preference has changed it shows the value in summary of onChange_Color
            Preference onChange_Color = findPreference(getString(R.string
                    .settings_change_color_key));
            bindPreferenceSummaryToValue(onChange_Color);

            String clientID = client.getAuth().getUser().getId();
            Preference user_id = findPreference("user_id");
            user_id.setSummary(clientID);

            try{

                user_id.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
                    @Override
                    public boolean onPreferenceClick(Preference preference) {
                        clickCounter ++;

                        if(clickCounter==3){

                            setIDasClipData(getActivity(),clientID);
                            clickCounter=0;
                        }

                        return true;
                    }

                });

            }catch (Exception e){
                Log.e(LOG_TAG," Error: " + e);
            }


            SwitchPreferenceCompat soundSwitch = (SwitchPreferenceCompat) findPreference("pref_sound");

            if (soundSwitch != null) {

                if (soundSwitch.isChecked()) {
                    soundSwitch.setTitle(getString(R.string.turn_off_the_sound));
                } else {
                    soundSwitch.setTitle(getString(R.string.turn_on_the_sound));
                }

                soundSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                    @Override
                    public boolean onPreferenceChange(Preference preference, Object isSoundOnObject) {
                        boolean isVibrateOn = (Boolean) isSoundOnObject;
                        if (isVibrateOn) {
                            soundSwitch.setTitle(getString(R.string.turn_off_the_sound));
                        } else {
                            soundSwitch.setTitle(getString(R.string.turn_on_the_sound));
                        }
                        return true;
                    }

                });
            }

        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            //HERE CAN'T SET LIST DIVIDERS
            setDivider(getResources().getDrawable(R.drawable.list_divider));
            setDividerHeight(4);

        }


        @Override
        public void onCreatePreferences(Bundle bundle, String s) {


        }

        private static void setIDasClipData(Context c,String textToCopy){
            final ClipboardManager clipboardManager =(ClipboardManager) c.getSystemService(CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("", textToCopy);
            clipboardManager.setPrimaryClip(clipData);

            Toast ToastMessage = Toast.makeText(c, c.getString(R.string.client_id_been_copied_to_tray),  Toast.LENGTH_LONG);
            View toastView = ToastMessage.getView();
            toastView.setBackgroundResource(R.drawable.toast_custom);
            ToastMessage.show();


        }

        //help Method
        private void bindPreferenceSummaryToValue(Preference preference)
        {
            preference.setOnPreferenceChangeListener((Preference.OnPreferenceChangeListener) this);
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences
                    (preference.getContext());
            String preferenceString = preferences.getString(preference.getKey(), "");
            onPreferenceChange(preference, preferenceString);

        }

        //If preference has changed
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            String stringValue = newValue.toString();
            //if preference instanceof ListPreference set summary to this
            if (preference instanceof ListPreference)
            {
                ListPreference listPreference = (ListPreference) preference;
                int            prefIndex      = listPreference.findIndexOfValue(stringValue);
                if (prefIndex >= 0)
                {
                    CharSequence[] labels = listPreference.getEntries();
                    preference.setSummary(labels[prefIndex]);
                }
                //else set summary to String value
            }
            else
            {
                preference.setSummary(stringValue);
            }
            return true;
        }
    }
/*
 * making a menu code
 */
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.top_icons_settings, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp()
{
    onBackPressed();
    Intent mainIntent = new Intent(this, MainActivity.class);
    startActivity(mainIntent);
    return true;
}

}

settings_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:title="title">

    <android.support.v7.preference.ListPreference
            android:dialogTitle="@string/settings_color_label"
            android:defaultValue="@string/settings_change_color_default"
            android:entries="@array/settings_color_label"
            android:entryValues="@array/settings_color_values"
            android:key="@string/settings_change_color_key"
            android:title="@string/settings_color_label"
    />


    <android.support.v7.preference.SwitchPreferenceCompat
            android:key="pref_sound"
            android:title="@string/turn_off_the_sound"
            android:defaultValue="true" />

    <android.support.v7.preference.Preference
            android:title="@string/user_id_settings"
            android:key="user_id"
            android:summary="---">
    </android.support.v7.preference.Preference>

    <android.support.v7.preference.Preference
            android:title="@string/program_version"
            android:key="program version"
            android:summary="1.0">
    </android.support.v7.preference.Preference>

    <android.support.v7.preference.PreferenceScreen
            android:title="@string/privacy_policy_title"
            android:summary="@string/privacy_policy_tap_toopen">
        <android.support.v7.preference.Preference
                android:key="privacy"
                android:title="@string/privacy_policy_title"
                android:summary="@string/privacy_policy_summary"/>
    </android.support.v7.preference.PreferenceScreen>



</android.support.v7.preference.PreferenceScreen>

settings_fragment.xml:

 <?xml version="1.0" encoding="utf-8"?>
    <fragment
            android:id="@+id/settings_activity_fragment"
            android:name="project.ironmax.com.ironmax_guard.SettingsActivity$MainPreferenceFragment"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="project.ironmax.com.ironmax_guard.SettingsActivity"
            android:clickable="true"
            android:background="@drawable/ripple">
    </fragment>

要显示填充和分隔线

0 个答案:

没有答案