如何构建自定义首选项扩展android.support.v7.preference库?

时间:2017-02-01 11:26:35

标签: android android-custom-view android-preferences preferenceactivity

在这些日子里,我正在开发一个包含首选项的应用程序,我试图将每个首选项放入CardView。我可以使用旧版本的首选项库进行自定义首选项,但是当我想使用CardView时,我必须使用首选项库v7。
Screen Shot
如您所见,我使用CardView创建了一个首选项类别。首先,我使用包含片段的CardView进行了首选活动。然后我在我的ActivityPrefrences中添加我的偏好文件

public class ActivityPrefrences extends AppCompatActivity {

private int[] prefResId = {R.xml.app_preferences,R.xml.app_preferences};
private int[] fragmentResId = {R.id.fragmentGeneralSetting,R.id.fragmentRtl};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_preference);

    if (savedInstanceState == null) {

        for(int i=0;i<prefResId.length;i++){
            Fragment fragment = new PreferenceDataFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("PREFERENCE_KEY", prefResId[i]);
            fragment.setArguments(bundle);
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(fragmentResId[i],fragment).commit();
        }

    }
}


片段类:

public class PreferenceDataFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener{
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    //getPreferenceManager().setSharedPreferencesName("settings");
    //getPreferenceManager().setSharedPreferencesMode(Context.MODE_PRIVATE);

    Bundle bundle = this.getArguments();
    if (bundle != null) {
        int resId = bundle.getInt("PREFERENCE_KEY", R.xml.app_preferences);
        addPreferencesFromResource(resId);
    }
  }
}

activity_preference.xml

 <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbarThumbVertical="@drawable/scrollbar_thumb"
    android:scrollbarTrackVertical="@drawable/scrollbar_track"
    android:clipToPadding="false"
    android:duplicateParentState="false"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            app:cardElevation="6dp"
            app:cardCornerRadius="6dp"
            app:cardBackgroundColor="@android:color/white">

            <FrameLayout
                android:id="@+id/fragmentGeneralSetting"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            app:cardElevation="6dp"
            app:cardCornerRadius="6dp"
            app:cardBackgroundColor="@android:color/white">

            <FrameLayout
                android:id="@+id/fragmentRtl"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </android.support.v7.widget.CardView>
    </LinearLayout>
</ScrollView>


最后我在preference.xml中添加了我自己的自定义首选项,但是当我在手机上加载此活动时,我错过了屏幕上的一部分视图。正如您在上面的图像中看到的,灰色区域是持有者,白色是自定义偏好视图的一部分。
现在我无法理解为什么我的自定义视图不适合整个持有人区域?而且我不能在课堂上使用setSummary和setTitle,实际上“标题”和“摘要”没有应用于TextViews,我不知道为什么?

这是我的自定义偏好类

public class RTLCheckBoxPreference extends Preference {

private OnPreferenceClickListener prefListener;
private boolean currentValue = false;

public RTLCheckBoxPreference(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public RTLCheckBoxPreference(Context context, AttributeSet attrs,
                             int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setWidgetLayoutResource(R.layout.prefrence_checkbox);
}

@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
    super.onSetInitialValue(restorePersistedValue, defaultValue);
    currentValue = restorePersistedValue ? getPersistedBoolean(false):(Boolean) defaultValue;
}

@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
    super.onBindViewHolder(holder);
    holder.itemView.setClickable(false);
    holder.itemView.setPadding(0,0,0,0);

    RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
    param.setMargins(0,0,0,0);
    holder.itemView.setLayoutParams(param);
  }
}


这是我的偏好的自定义布局。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:minHeight="?attr/listPreferredItemHeight"
              android:gravity="center_vertical"
              android:paddingRight="?android:attr/scrollbarSize"
              android:background="?attr/selectableItemBackground"
              android:focusable="true" >

    <com.bvapp.widget.HoloButton.HoloButton
        android:id="@+id/btnCheckBoxPrefrence"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:layout_marginTop="6dip">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:background="@drawable/check_box_outline_blank"
            android:id="@+id/imgCheckBoxPrefrence"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>

        <TextView android:id="@android:id/title"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:singleLine="true"
                  android:textAppearance="?android:attr/textAppearanceLarge"
                  android:textColor="@color/text_main"
                  android:ellipsize="marquee"
                  android:fadingEdge="horizontal"
                  android:gravity="right"
                  android:layout_alignParentEnd="true"
                  android:layout_alignParentRight="true"
                  android:textSize="@dimen/font_norm_size"/>

        <TextView android:id="@android:id/summary"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@android:id/title"
                  android:layout_alignRight="@android:id/title"
                  android:textAppearance="?android:attr/textAppearanceSmall"
                  android:textColor="@color/text_aux"
                  android:maxLines="4"
                  android:gravity="right"
                  android:textSize="@dimen/font_aux_norm_size"/>

    </RelativeLayout>

</FrameLayout>

我尝试了很多找到我的答案,即使我试图将填充和边距设置为“0”,但它不起作用。我应该如何在持有人中使用我自己的自定义偏好并在我的自定义首选项中使用“标题”和“摘要”?

0 个答案:

没有答案
相关问题