android – 具有离散值的SeekBarPreference()

android – 具有离散值的SeekBarPreference(),第1张

概述我只是想在我的项目中包含一个只有3个值的SeekBarPreference(紧急程度:低 – 中 – 高) 这可以很容易地在经典的SeekBar上完成(参见下面的示例),但我不知道如何能够包含偏好. 如果我使用这个代码,它将在SeekBar(不是Preference)中工作,但在Preferences中它仍然会显示没有粗标记的SeekBar. 码: <SeekBar android:id 我只是想在我的项目中包含一个只有3个值的SeekbarPreference(紧急程度:低 – 中 – 高)

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

如果我使用这个代码,它将在Seekbar(不是Preference)中工作,但在Preferences中它仍然会显示没有粗标记的Seekbar.

码:

<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" />

有什么想法或建议吗?

解决方法 您需要使用支持SeekbarPreference来自定义拇指和所有.您可以为自己的搜索栏偏好传递自己的自定义布局.请查看下面的示例.

您的活动,您将添加您的偏好片段

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
总结

以上是内存溢出为你收集整理的android – 具有离散值的SeekBarPreference()全部内容,希望文章能够帮你解决android – 具有离散值的SeekBarPreference()所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1126139.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存