android - 样式化 SeekBarPreference 以与 SwitchPreference 保持一致

标签 android android-preferences android-styles

我的 preferences.xml 中有以下首选项:

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

问题是这会呈现 MarglinsSwitchPreference 的标题非常不同的样式:

1]

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

最佳答案

在你的主题中,尝试设置

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

在我的模型中,这显示了以下内容:

enter image description here

这是使用 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
只是偏好 fragment 的家。

<?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 :

The seekbar value view can be shown or disabled by setting showSeekBarValue attribute to true or false, respectively.

不幸的是,在 app_preferences.xml 文件中设置此值会给出“是私有(private)的”错误。我所看到的也没有公共(public)方法来设置控制它的内部变量。您可以继承 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" />

您会看到该值不再显示。

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

关于android - 样式化 SeekBarPreference 以与 SwitchPreference 保持一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51194441/

相关文章:

android - 发布版本 : transformClassesAndResourcesWithProguardForRelease FAILED

android - RecyclerView-设置它和项目的透明度

Android _ import android.support.v7.graphics 无法解决?

android-layout - Android - DialogPreferenec 只有一个按钮

android - 根据带有兼容库的状态更改 FAB 的图标颜色

android - NestedSCrollView 在 CoordinatorLayout 中没有完全滚动

java - Android:AdapterView 不支持 addView(View)

android - 如何将复制的文本传递给 Android 中的其他 Activity

android - 如何在 Android 上以编程方式将样式设置为 progressBar

java - 在 Android 中更改 Switch 的颜色