首选项屏幕中的文本大小[英] Text size within an PreferenceScreen

本文是小编为大家收集整理的关于首选项屏幕中的文本大小的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有XML文件,它定义了一些像以下示例的一些首选项屏幕

<PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android"          
   android:key="root_preferencescreen">

    <PreferenceScreen  android:key="general_sett" android:title="general settings" />
     ....

    <PreferenceScreen  android:key="extras_sett" android:title="extras settings" />

</PreferenceScreen> 

我希望能够增加首选项屏幕的文本的字体大小,但是因为在偏好屏幕内没有Android:textsize标记,我不知道如何完成这个!

推荐答案

您可以制作一个TextView布局xml,查看类似的东西:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/listSeparatorTextViewStyle"
    android:textColor="@android:color/white"
    android:id="@+android:id/title"
/>

并在preferences.xml中设置您的首选项类别的布局:

<PreferenceCategory
    android:title="Category Title"
    android:layout="@layout/pref_category"
/>

只要TextView具有id @+android:id/title,您可以使您想要的布局看起来.还有一种方法可以用我弄清楚的风格来做这件事.

其他推荐答案

您可以简单地在主题内添加android:textSize以获取首选项屏幕:

e.g:

<style name="settingsTheme" parent="PreferenceThemeOverlay">
    <item name="colorAccent">@color/color_ten</item>
    <item name="android:background">@color/colorPrimaryLight</item>
    <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
    <item name="android:textColorPrimary">@color/colorTextBodyLight</item>
    <item name="android:textColorSecondary">@color/colorTextCaptionLight</item>
    <item name="android:textSize">14sp</item>
</style>

你的SettingsActivity类:

public class SettingsActivity extends AppCompatActivity {
private Toolbar toolbar;

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

    toolbar = (Toolbar)findViewById(R.id.toolbar_settings);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.bodylayout, new PrefsFragment())
            .commit();


}

@Override
protected void onPause() {
    super.onPause();
}
}

PrefsFragment类:

public class PrefsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        final Context myContext = this.getActivity();
    //set your theme
        myContext.setTheme(R.theme.settingsTheme);

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settingspreference);
    //do other stuffs
        }
    //.....
        }

本文地址:https://www.itbaoku.cn/post/102486.html

问题描述

I have xml file that defines some preference screens like the following example

<PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android"          
   android:key="root_preferencescreen">

    <PreferenceScreen  android:key="general_sett" android:title="general settings" />
     ....

    <PreferenceScreen  android:key="extras_sett" android:title="extras settings" />

</PreferenceScreen> 

I would like to be able to increase the font size for the text of the preference screen , but because the within a preference screen there is no android:textsize tag , i have no idea how to accomplish that !

推荐答案

You can make a TextView layout xml that looks something like this:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/listSeparatorTextViewStyle"
    android:textColor="@android:color/white"
    android:id="@+android:id/title"
/>

and set the layout of your preference category in your preferences.xml like this:

<PreferenceCategory
    android:title="Category Title"
    android:layout="@layout/pref_category"
/>

As long as the TextView has the id @+android:id/title you can make the layout look however you want. There is also a way to do this with styles that I haven't quite figured out.

其他推荐答案

You can simply add android:textSize inside your theme for preference screen:

e.g :

<style name="settingsTheme" parent="PreferenceThemeOverlay">
    <item name="colorAccent">@color/color_ten</item>
    <item name="android:background">@color/colorPrimaryLight</item>
    <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item>
    <item name="android:textColorPrimary">@color/colorTextBodyLight</item>
    <item name="android:textColorSecondary">@color/colorTextCaptionLight</item>
    <item name="android:textSize">14sp</item>
</style>

Your SettingsActivity class :

public class SettingsActivity extends AppCompatActivity {
private Toolbar toolbar;

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

    toolbar = (Toolbar)findViewById(R.id.toolbar_settings);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.bodylayout, new PrefsFragment())
            .commit();


}

@Override
protected void onPause() {
    super.onPause();
}
}

PrefsFragment class :

public class PrefsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        final Context myContext = this.getActivity();
    //set your theme
        myContext.setTheme(R.theme.settingsTheme);

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settingspreference);
    //do other stuffs
        }
    //.....
        }