【问题标题】:Styling SeekBarPreference to be consistent with SwitchPreference将 SeekBarPreference 样式设置为与 SwitchPreference 一致
【发布时间】:2018-07-05 14:56:19
【问题描述】:

我的preferences.xml 中有以下偏好:

<SwitchPreference
    android:summary="Lorum ipsum dolor sit amet"
    android:title="Frobulate" />
<SeekBarPreference android:title="Marglins"/>
<SwitchPreference android:title="Bromzuling" />

这样做的问题是,这会使Marglins 的样式与SwitchPreferences 的标题截然不同:

我可以在我的styles.xml 中添加一些东西来使标题在字体大小、颜色、对齐方式等方面看起来相同吗?

【问题讨论】:

  • 检查thisthis
  • 我将无耻地插入my library,所有半生不熟的东西都应该开箱即用。

标签: android android-preferences android-styles


【解决方案1】:

在您的主题中,尝试设置

<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>

在我的模型中,显示如下:

这是使用 com.android.support:preference-v7:27.1.1。由于这是您正在寻找的外观,因此请尽可能使用此库。

确保您始终使用偏好支持库而不是混淆;否则,事情不会像预期的那样看起来/工作。

这是一个演示 SeekBar 首选项样式的小应用程序。除了显示首选项之外,该应用程序实际上并没有做任何事情。此应用显示与上图相同的显示。

AndroidManifest.xml
这里没什么特别的。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.preferencecustomlayout">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name="com.example.preferencecustomlayout.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

styles.xml

<resources>
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
            <!-- Theme for the preferences -->
            <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
        </style>
    </resources>

app_preferences.xml

<?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.SwitchPreferenceCompat
        android:key="switchPreference1"
        android:summary="Lorum ipsum dolor sit amet"
        android:title="Frobulate" />

    <android.support.v7.preference.SeekBarPreference
        android:key="seekBarPreference"
        android:title="Marglins" />

    <android.support.v7.preference.SwitchPreferenceCompat
        android:key="switchPreference1"
        android:title="Bromzuling" />

</android.support.v7.preference.PreferenceScreen>

MainActivity.java
注意顶部的所有“v7”导入。不要让这些逃脱。如果事情不起作用,请检查您是否仍在使用支持库。

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.PreferenceFragmentCompat;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            Fragment preferenceFragment = new PrefsFragment();
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(R.id.prefContainer, preferenceFragment);
            ft.commit();
        }
    }

    public static class PrefsFragment extends PreferenceFragmentCompat {

        @Override
        public void onCreatePreferences(Bundle bundle, String s) {
            addPreferencesFromResource(R.xml.app_preferences);
        }
    }
}

activity_main.xml
只是偏好片段的归宿。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/prefContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.preferencecustomlayout.MainActivity" />

至于从搜索栏中消除数字显示,这将涉及更多内容。根据SeekBarPreference documentation

可以通过将 showSeekBarValue 属性分别设置为 true 或 false 来显示或禁用搜索栏值视图。

不幸的是,在app_preferences.xml 文件中设置此值会导致“私有”错误。我也没有看到任何公共方法来设置控制它的内部变量。你可以继承SeekBarPreference,覆盖onBindViewHolder(),如下:

MySeekBarPreference.java

import android.content.Context;
import android.support.v7.preference.PreferenceViewHolder;
import android.support.v7.preference.SeekBarPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;


public class MySeekBarPreference extends SeekBarPreference {
    public MySeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public MySeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MySeekBarPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySeekBarPreference(Context context) {
        super(context);
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder view) {
        super.onBindViewHolder(view);
        TextView seekBarValueTextView = (TextView) view.findViewById(R.id.seekbar_value);
        seekBarValueTextView.setVisibility(View.GONE);
    }
}

上述自定义搜索栏首选项类将摆脱搜索栏值。将app_preferences.xml中的搜索栏定义更改为:

<com.example.preferencestyleseekbar.MySeekBarPreference
    android:key="seekBarPreference"
    android:title="Marglins" />

您会看到该值不再显示。

偏好通常是一团糟。我找到了 Jakob Ulbrich 的一个非常好的series of articles,关于偏好以及让它们工作并看起来像材料设计。您可能会发现检查它们很有帮助。

【讨论】:

  • 目前它不显示数值,只有一个滑块,我想保持这种状态;你推荐的这个主题有可能吗?
  • 我尝试将preferenceTheme 项目添加到我的清单中为偏好活动指定的style 中,但它似乎没有任何改变。你能展示一个 styles.xmlAndroidManifest.xml 片段来展示它们应该如何连接在一起吗?
【解决方案2】:

对我来说,解决方案是使用 com.android.support:preference-v14 而不是 v7。这允许我使用 PreferenceThemeOverlay.v14.Material,否则无法访问。

【讨论】:

    猜你喜欢
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多