在您的主题中,尝试设置
<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,关于偏好以及让它们工作并看起来像材料设计。您可能会发现检查它们很有帮助。