简体   繁体   English

当我宣布它为浅色主题时,为什么会出现深色主题?

[英]Why am I getting Dark theme when I have declared it to be light?

I'm developing an app with material design. 我正在开发具有材质设计的应用程序。

I have added a toolbar to the activity & I have set it's theme to be light here: app:popupTheme="@style/ThemeOverlay.AppCompat.Light" , but still I'm getting a dark theme after running it. 我在活动中添加了一个工具栏,并在此处将其主题设置为浅色: app:popupTheme="@style/ThemeOverlay.AppCompat.Light" ,但运行该主题后我仍然得到一个黑暗的主题。

Here's my activity_about.xml file's code: 这是我的activity_about.xml文件的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.abc.xyz.AboutActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

</RelativeLayout>

Here's AboutActivity.java file's code: 这是AboutActivity.java文件的代码:

public class AboutActivity extends AppCompatActivity {

    Toolbar toolbar;

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

        SpannableString s = new SpannableString("About");
        s.setSpan(new TypefaceSpan(this, "Pacifico.ttf"), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(s);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_about, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Here's styles.xml file's code: 这是styles.xml文件的代码:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

I'm new to material design & hence don't know what to do. 我是材料设计的新手,因此不知道该怎么做。

Please let me know. 请告诉我。

I'm new to StackOverflow, so please cooperate. 我是StackOverflow的新手,所以请配合。

Thanks in advance. 提前致谢。

You have to change you xml AppTheme . 您必须更改xml AppTheme

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    app:theme="@style/ThemeOverlay.Material.Light.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

You have to make Toolbar theme in your xml file 您必须在xml文件中制作工具栏主题

In styles.xml 在styles.xml中

 <style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Light">

    <item name="colorPrimary">@color/seccolor</item>
    <item name="colorControlHighlight">@color/md_white_1000</item>
</style>

And Now In your Main xml File use it 现在在您的主xml文件中使用它

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?attr/colorPrimaryDark"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Light"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:layout_gravity="center_horizontal|top" />

It should Work. 它应该工作。

The app:popupTheme attribute is for the menu popup, and all other popups that overlay the toolbar. app:popupTheme属性用于菜单弹出窗口以及覆盖工具栏的所有其他弹出窗口。

May be your toolbar is not getting themed because you haven't referenced the correct theme in AndroidManifest.xml . 可能是因为您没有在AndroidManifest.xml引用正确的主题,所以您的工具栏没有主题化。

Make sure it is like this: 确保是这样的:

<manifest>
    <application
        ...
        android:theme="@style/MyMaterialTheme.Base">

        <activity/>
        ...

    </application>
</manifest>

That'll theme all the activities in your app. 这将成为您应用程序中所有活动的主题。 You can even apply it on individual activities. 您甚至可以将其应用于单个活动。

In your styles.xml 在你的styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

</style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

the parent of your main theme is 您主要主题的父级是

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

also in your MyMaterialTheme.Base has parent as 也在您的MyMaterialTheme.Base父级设置为

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">

Removing the .DarkActionBar from both will help. 同时删除.DarkActionBar会有所帮助。 Then you can probably add a style for your toolbar as mentioned in above answers. 然后,您可以为工具栏添加一种样式,如上面的答案所述。

Hope it helps. 希望能帮助到你。

The problem here was that I was not applying a theme for toolbar. 这里的问题是我没有为工具栏应用主题。

I fixed this by adding just this line: android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" to my toolbar widget in xml files. 我通过添加以下行来修复此问题: android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"到xml文件中的工具栏小部件中。

Thanks all for your efforts! 感谢大家的努力!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到一个变量未声明的错误? - Why am I getting a varriable not declared error? 如何使应用程序具有黑暗和明亮的主题? - How can I make an app dark and light theme? 为什么我的程序“必须被捕获或被宣布被抛出”? - Why am I getting "must be caught or declared to be thrown" on my program? 当我正确导入所述类并在其目录中查看它时,为什么我得到ClassNotFoundExpection? - Why am I getting ClassNotFoundExpection when I have properly imported said class and am looking at it in its directory? 为什么当 while(true) 直接声明同时 while(isValid=true) 会丢失 return 错误时,我没有得到丢失的 return 语句? - Why am I not getting a missing return statement when while(true) is declared directly meanwhile while(isValid=true) will get missing return error? 当我已经定义了bean时,为什么会出现NoSuchBeanDefinitionException? - Why am I getting a NoSuchBeanDefinitionException when I have already defined the bean? windowbackground 不适用于深色和浅色主题 - windowbackground not work in dark and light theme 为什么我在解密时收到“BadPaddingException”? - Why am I getting 'BadPaddingException' when decrypting? 为什么我得到 SAXparseException “必须声明元素类型”,即使它是在将 XML 文件加载到属性 Object 时声明的? - why am I getting SAXparseException “Element type must be declared” eventhough it was declared while loading XML file to Properties Object? 为什么从对象获取信息时会出现 StackOverflowError? - Why am I getting a StackOverflowError when getting information from an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM