android - 使工具栏文本颜色为白色并防止重叠

标签 android android-toolbar

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

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

我还尝试将文本颜色设为白色: 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>

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

最佳答案

请为 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" />

关于android - 使工具栏文本颜色为白色并防止重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35013583/

相关文章:

android - 是否有任何选项可以在工具栏右侧添加 TICK MARK?

android - 如何删除appbarlayout android中工具栏顶部的额外填充

android - Material Design - AppCompat 工具栏不显示阴影

android - AppBarLayout 边框处有奇怪的阴影

android - 什么时候在android中使用不推荐使用的方法是合适的

Android:Google MapView 在加载 map 时显示进度指示器

android - 如何同步firebase数据?

android - 调用 bindAppWidgetId 时出现安全异常

Android Gradle 构建错误

java - 将 TextView 添加到 Toolbar 的元素