创建自定义首选项

问题描述 投票:0回答:1

我想创建类似SeekBarPreference的首选项。我需要范围搜索栏,所以我找到了这个库:https://github.com/syedowaisali/crystal-range-seekbar。我想使用CrystalRangeSeekbar创建一个与其他首选项具有相同外观的首选项。

我使用compileSdkVersion = 29,minSdkVersion = 23,targetSdkVersion = 29。

甚至在Android上可能吗?我花了几个小时尝试编写某些东西,但从未设法获得相同的外观。我需要所有的填充,边距,标题和摘要位置等都与内置首选项中的相同。

是否有任何官方手册可以使自定义首选项的外观与内置首选项相同?

android android-layout android-preferences
1个回答
0
投票

我认为没有有关此操作方法的手册,但是我遇到了一个问题,即我需要minSdkVersion = 22的远程搜索栏首选项,而当时可用的东西直到后来的SDK才允许远程搜索。版本。

所以我只是制作了自己的,不确定与内置的首选项相比看起来如何

使用xml布局代码段是

<GridLayout
            android:id="@+id/GridLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnCount="2"
            android:orientation="horizontal">

<SeekBar
                android:id="@+id/fontSeek"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_columnWeight="15"
                android:layout_gravity="fill"
                android:max="40"
                android:padding="6dp"/>


            <TextView
                android:id="@+id/fontText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_gravity="center"
                android:textStyle="bold"
                android:textColor="@color/colorText"
                android:typeface="monospace"
                android:minEms="2"
                android:padding="6dp"/>

</GridLayout>

然后在代码中,我将textview值调整为显示调整后的范围,而不是通过将值加20来显示0至40的范围。

与xml关联的提取的Java代码

int fontSize;

int fontSizeOffset = 20;

fontSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // Work out the correct values
                fontSizeAdjusted = progress;
                fontSize = fontSizeAdjusted + fontSizeOffset;
                // Update the Textview
                fontTextView.setText(String.valueOf(fontSize));
                // store pref
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putInt("fontSize", fontSize);
                editor.apply();
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
                // Do Nothing
            }

            public void onStopTrackingTouch(SeekBar seekBar) {
                // Do Nothing
            }
        });

您现在可以使用Androidx https://developer.android.com/reference/androidx/preference/SeekBarPreference做得更好,但是当我开发此代码时,此功能不可用。

您应该能够调整样式以使其与标准首选项完全匹配,因为文档说它只是带有可选texview的搜索栏。

© www.soinside.com 2019 - 2024. All rights reserved.