我的preferences.xml中有以下首选项
<SwitchPreference
android:summary="Lorum ipsum dolor sit amet"
android:title="Frobulate" />
<SeekBarPreference android:title="Marglins"/>
<SwitchPreference android:title="Bromzuling" />这样做的问题是,这会使Marglins呈现出与SwitchPreference标题截然不同的风格:

有没有什么东西可以放在我的styles.xml中,使标题在字体大小、颜色、对齐方式等方面看起来都一样?
发布于 2018-08-03 06:26:21
在您的主题中,尝试设置
<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文件中设置此值会产生"is private“错误。据我所知,也没有公共方法来设置控制此操作的内部变量。您可以子类化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,关于偏好,让它们工作,看起来像材料设计。你可能会发现查看它们是有帮助的。
发布于 2018-08-11 18:30:22
对我来说,解决方案是使用com.android.support:preference-v14而不是v7。这允许我使用PreferenceThemeOverlay.v14.Material,否则它是不可访问的。
https://stackoverflow.com/questions/51194441
复制相似问题