gpt4 book ai didi

android - 具有离散值的 SeekBarPreference ()

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:36 29 4
gpt4 key购买 nike

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

这可以在经典的 SeekBar 上轻松完成(请参见下面的示例),但我不知道如何将其包含在首选项中。

如果我使用这段代码,它将在 SeekBar(不是首选项)中工作,但在首选项中它仍然会显示一个没有粗标记的 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" />

有什么想法或建议吗?

enter image description here

最佳答案

您需要使用支持 SeekbarPreference 来自定义 thumb 和所有。您可以根据自己的搜索栏首选项传入自己的自定义布局。请检查下面的示例。

您将在其中添加偏好 fragment 的 Activity

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 在 list 中添加特定主题

<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 加载首选项的 fragment

空白 fragment .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 (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47672431/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com