具有离散值的SeekBarPreference()

时间:2017-12-06 10:48:12

标签: android android-preferences seekbarpreference

我只是想在我的项目中包含一个只有3个值的SeekBarPreference(紧急程度:低 - 中 - 高)

这可以很容易地在经典的SeekBar上完成(参见下面的示例),但我不知道如何能够包含偏好。

如果我使用此代码,它将在SeekBar(不是Preference)中工作,但在Preferences中它仍会显示没有粗标记的SeekBar。

CODE:

<SeekBar
     android:id="@+id/sb"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:max="10"
     android:thumb="@drawable/ic_location"
     android:theme="@style/Widget.AppCompat.SeekBar.Discrete" />

有任何想法或建议吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要使用支持SeekbarPreference来自定义thumb和所有内容。您可以为自己的搜索栏偏好传递自己的自定义布局。请查看下面的示例。

您要添加偏好片段的活动

public class MainActivity extends AppCompatActivity {

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

        BlankFragment mPrefsFragment = new BlankFragment();

        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction mFragmentTransaction = mFragmentManager
                .beginTransaction();
        mFragmentTransaction.replace(android.R.id.content, mPrefsFragment);
        mFragmentTransaction.commit();

    }
}

您必须在此活动的清单中添加特定主题

<activity
    android:name=".MainActivity"
    android:theme="@style/AppTheme.SettingsTheme" />

<强> styles.xml

<style name="AppTheme.SettingsTheme" parent="AppTheme.NoActionBar">
    <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>

从xml加载首选项的片段

<强> BlankFragment.java

public class BlankFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preferences, rootKey);
    }
}

您的偏好文件来自 xml res文件夹

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

    <android.support.v7.preference.SeekBarPreference
        android:key="your_pref_key"
        android:max="3"
        android:thumb="@drawable/thumb_icon"
        android:summary="Summary"
        android:layout="@layout/my_custom_seekbar"
        android:title="Title" />
</android.support.v7.preference.PreferenceScreen>

在这里,您可以看到android:layout属性采用资源布局文件,您可以在其中提供自己的自定义搜索栏和textview,其中显示了搜索栏中的选定值

<强> my_custom_seekbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/seekbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/seekbar_value"
        android:max="10"
        android:theme="@style/Widget.AppCompat.SeekBar.Discrete"
        android:thumb="@drawable/ic_location" />

    <TextView
        android:id="@+id/seekbar_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />
</RelativeLayout>

请注意,您必须为自定义使用完全相同的标识符 SeekBar和TextView,否则会抛出NullPointerException

    android:id="@+id/seekbar" for your custom Seekbar
    android:id="@+id/seekbar_value" for your custom TextView
相关问题