【问题标题】:Make toolbar text color white and prevent overlapp使工具栏文本颜色为白色并防止重叠
【发布时间】:2016-05-03 00:01:21
【问题描述】:

我想在我的设置页面中包含一个工具栏并按照以下说明进行操作: https://stackoverflow.com/a/27455330/2977288(第一部分)

由于某种原因,工具栏与内容重叠:

我还尝试将文本颜色设为白色: https://stackoverflow.com/a/26555178/2977288

但正如您在屏幕截图中看到的,颜色仍然是黑色。

这是我正在使用的代码:

settings_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:title="@string/action_settings"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
        Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);
        root.addView(bar, 0); // insert at top
        bar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

}

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <PreferenceCategory android:title="@string/pref_main">
        <CheckBoxPreference
            android:key="vibration"
            android:title="@string/pref_vibration"
            android:summary="@string/pref_summary_vibration"
            android:defaultValue="true" />
        <Preference
            android:key="show_wizard"
            android:title="@string/pref_wizard"
            android:summary="@string/pref_summary_wizard">
            <intent
                android:action="android.intent.action.VIEW"
                android:targetPackage="de.tum.dstrctrl"
                android:targetClass="de.tum.dstrctrl.activities.WizardActivity" />
        </Preference>
    </PreferenceCategory>

</PreferenceScreen>

有人对这两个问题之一(或两者都有)有想法吗?

【问题讨论】:

  • 请出示您的xml ,其中包含toolbar
  • @MustansarSaeed 这是第一个 xsml 文件 (settings_toolbar.xml)
  • 您也将此settings_toolbar 布局包含在另一个xml 中。请显示该xml
  • 它是在 SettingsActivity.java 中加载的,而不是在另一个 xml 文件中。就像在链接堆栈答案中一样

标签: android android-toolbar


【解决方案1】:

请使用PreferenceActivity 的自定义布局,并在该布局中包含toolbarHow to add ToolBar in PreferenceActivity? 上建议的其他内容。

希望这会有所帮助。

更新 您可以通过创建自定义样式来更改工具栏的标题颜色,然后在工具栏中使用该自定义布局

<style name="ActionBar" parent="Theme.AppCompat">
        <item name="android:background">@color/actionbar_background</item>
        <item name="android:titleTextStyle">@style/ActionBar.TitleTextStyle</item>
    </style>

    <style name="ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_title_text</item>
    </style>

settings_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:title="@string/action_settings"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    style="@style/ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

【讨论】:

  • 为什么它不适用于这个答案? stackoverflow.com/a/27455330/2977288
  • PreferenceActivitygetFragmentManager().beginTransaction() .replace(android.R.id.content, new SettingsFragment()) .commit();的作用是什么,你要把onCreate中的那行加成addPreferencesFromResource(R.xml.preferences);
  • 我现在通过使用developer.android.com/guide/topics/ui/settings.html#Activity 而不是片段来解决它。所以至少你说的是......关于颜色的任何想法?
  • 这就是我刚刚分享的,你不必使用片段。我在最后测试了您的代码,只是删除了片段内容并添加了 addPreferencesFromResource(R.xml.preferences); 行,它运行良好。不需要片段
  • 我只是使用了片段,因为Android Doc推荐它。关于文本颜色的任何想法?还是黑了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多