Android SeekBarPreference与离散值()
我只想在我的项目中包含一个SeekBarPreference,它只有3个值(紧急级别:低-中-高) 这可以很容易地在一个经典的SeekBar上完成(见下面的示例),但我不知道如何才能将其作为首选 如果我使用这段代码,它将在SeekBar(不是首选项)中工作,但在首选项中,它仍将显示没有粗标记的SeekBar 代码:Android SeekBarPreference与离散值(),android,android-preferences,seekbarpreference,Android,Android Preferences,Seekbarpreference,我只想在我的项目中包含一个SeekBarPreference,它只有3个值(紧急级别:低-中-高) 这可以很容易地在一个经典的SeekBar上完成(见下面的示例),但我不知道如何才能将其作为首选 如果我使用这段代码,它将在SeekBar(不是首选项)中工作,但在首选项中,它仍将显示没有粗标记的SeekBar 代码: 有什么想法或建议吗 您需要使用支持查看kbarpfreference来定制拇指和所有功能。您可以根据搜索栏偏好传入自己的自定义布局。请检查下面的示例 您将添加偏好片段的活动 pu
有什么想法或建议吗
您需要使用支持
查看kbarpfreference
来定制拇指和所有功能。您可以根据搜索栏偏好传入自己的自定义布局。请检查下面的示例
您将添加偏好片段的活动
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" />
和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>
请注意,您必须使用完全相同的标识符进行自定义
查看kbar和TextView,否则它将抛出NullPointerException
android:id="@+id/seekbar" for your custom Seekbar
android:id="@+id/seekbar_value" for your custom TextView
我编辑了这篇文章,但离散标记不会出现,seekbar将保持默认seekbarAccepted的状态,但有一些备注:1)在xml的根绝对布局中使用wrap_内容。2) 我将尝试找到原始布局,因为我们应该添加首选项的标题和摘要,在原始appcompat库源代码中哪里可以找到它们?1)请根据您的要求调整布局,我刚刚发布了一个工作示例。2) 我不明白你说的“原始布局”是什么意思。您可以在首选项XML中添加标题和摘要。您是指如果我们没有提供自定义布局,则显示的默认布局吗?那么您是对的,它可以在appcombat库源代码中找到。但是要设置标题和摘要,您可以在首选项xml本身中进行设置。@Emil和Waza__Be:根据这个很好的示例,我刚刚实现了一个SeekBarPreference,但是我看不到标题和摘要,就像前面提到的Waza_一样。如何使它们可见?
public class BlankFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
<?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>
<?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>
android:id="@+id/seekbar" for your custom Seekbar
android:id="@+id/seekbar_value" for your custom TextView