自定义首选项-填充无效,标题不可见

时间:2018-07-08 16:23:02

标签: android android-layout android-preferences

我正在尝试设置自定义首选项。填充无效,标题不可见。我尝试用LinearLayout包装小部件,但没有成功。我试图更改高度和宽度,但没有成功。当我从布局中删除RangeBar时,标题突然出现。

//自定义首选项类别

public class AgePreference extends Preference {
    private int age;

    // This is the constructor called by the inflater
    public AgePreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setWidgetLayoutResource(R.layout.preference_age);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

       /* RangeBar rangeBar = view.findViewById(R.id.rangebar);
        rangeBar.setSeekPinByIndex(age);
        rangeBar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
            @Override
            public void onRangeChangeListener(RangeBar rangeBar, int leftPinIndex, int rightPinIndex, String leftPinValue, String rightPinValue) {
                age = rightPinIndex;
                persistInt(age);
            }
        });*/

       /* // Set our custom views inside the layout
        final TextView myTextView = (TextView) view.findViewById(R.id.mypreference_widget);
        if (myTextView != null) {
            myTextView.setText(String.valueOf(age));
        }*/
    }

    @Override
    protected void onClick() {
       /* int newValue = age + 1;
        // Give the client a chance to ignore this change if they deem it
        // invalid
        if (!callChangeListener(newValue)) {
            // They don't want the value to be set
            return;
        }

        // Increment counter
        age = newValue;

        // Save to persistent storage (this method will make sure this
        // preference should be persistent, along with other useful checks)
        persistInt(age);

        // Data has changed, notify so UI can be refreshed!
        notifyChanged();*/
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        // This preference type's value type is Integer, so we read the default
        // value from the attributes as an Integer.
        return a.getInteger(index, 0);
    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        if (restoreValue) {
            // Restore state
            age = getPersistedInt(age);
        } else {
            // Set state
            int value = (Integer) defaultValue;
            age = value;
            persistInt(value);
        }
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        /*
         * Suppose a client uses this preference type without persisting. We
         * must save the instance state so it is able to, for example, survive
         * orientation changes.
         */

        final Parcelable superState = super.onSaveInstanceState();
        if (isPersistent()) {
            // No need to save instance state since it's persistent
            return superState;
        }

        // Save the instance state
        final SavedState myState = new SavedState(superState);
        myState.age = age;
        return myState;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (!state.getClass().equals(SavedState.class)) {
            // Didn't save state for us in onSaveInstanceState
            super.onRestoreInstanceState(state);
            return;
        }

        // Restore the instance state
        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());
        age = myState.age;
        notifyChanged();
    }

    /**
     * SavedState, a subclass of {@link BaseSavedState}, will store the state
     * of AgePreference, a subclass of Preference.
     * <p>
     * It is important to always call through to super methods.
     */
    private static class SavedState extends BaseSavedState {
        int age;

        public SavedState(Parcel source) {
            super(source);

            // Restore the click counter
            age = source.readInt();
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);

            // Save the click counter
            dest.writeInt(age);
        }

        public SavedState(Parcelable superState) {
            super(superState);
        }

        public static final Parcelable.Creator<SavedState> CREATOR =
                new Parcelable.Creator<SavedState>() {
                    public SavedState createFromParcel(Parcel in) {
                        return new SavedState(in);
                    }

                    public SavedState[] newArray(int size) {
                        return new SavedState[size];
                    }
                };
    }

} 

//首选项布局

<?xml version="1.0" encoding="utf-8"?>

<com.appyvet.materialrangebar.RangeBar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="@color/colorPrimary"
        android:id="@+id/rangebar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:mrb_pinMaxFont="10sp"
        app:mrb_rangeBarPaddingBottom="20dp"
        app:mrb_selectorBoundaryColor="@color/colorPrimaryDark"
        app:mrb_selectorBoundarySize="2dp"
        app:mrb_pinTextColor="@color/white"
        app:mrb_selectorSize="10dp"
        app:mrb_temporaryPins="true"
        app:mrb_tickEnd="60"
        app:mrb_tickInterval="1"
        app:mrb_tickStart="18"
        app:mrb_connectingLineColor="@color/colorPrimaryDark"
        app:mrb_pinColor="@color/colorPrimaryDark"
        app:mrb_tickColor="@color/colorPrimaryDark"
        app:mrb_selectorColor="@color/white"
        app:mrb_rangeBarColor="@color/colorPrimaryDark"
        app:mrb_tickHeight="0dp"
        app:mrb_rangeBar="false">
    </com.appyvet.materialrangebar.RangeBar>

// pref.xml

<com.example.jurajkostal.finalapp.Utils.AgePreference
        android:key="pref_max_volume"
        android:title="Test" />

0 个答案:

没有答案