【问题标题】:Why am I getting Dark theme when I have declared it to be light?为什么当我声明它是浅色时我会得到深色主题?
【发布时间】:2015-10-03 02:43:58
【问题描述】:

我正在开发一个具有材料设计的应用程序。

我在活动中添加了一个工具栏,并在此处将其主题设置为浅色:app:popupTheme="@style/ThemeOverlay.AppCompat.Light",但运行它后我仍然得到一个深色主题。

这是我的 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>

这是 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);
    }
}

这里是 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>

我是材料设计的新手,因此不知道该怎么做。

请告诉我。

我是 StackOverflow 的新手,所以请合作。

提前致谢。

【问题讨论】:

  • 请检查您的 style.xml 以了解您的应用主题!

标签: java android xml android-theme


【解决方案1】:

你必须改变你的 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"/>

【讨论】:

  • 将父主题从parent="Theme.AppCompat.Light.DarkActionBar" 更改为parent="Theme.AppCompat.Light.ActionBar" 后。上面写着:"Cannot resolve symbol: parent="Theme.AppCompat.Light.ActionBar""。现在该怎么办?
  • 检查编辑以获取更多信息并检查此link
【解决方案2】:

你必须在你的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>

现在在你的主 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" />

它应该工作。

【讨论】:

    【解决方案3】:

    app:popupTheme 属性用于菜单弹出窗口,以及覆盖工具栏的所有其他弹出窗口。

    可能是因为您没有在AndroidManifest.xml 中引用正确的主题,所以您的工具栏没有获得主题。

    确保它是这样的:

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

    这将为您的应用中的所有活动设置主题。您甚至可以将其应用于个人活动。

    【讨论】:

      【解决方案4】:

      在你的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>
      

      你的主题的父主题是

      &lt;style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"&gt;

      在你的MyMaterialTheme.Base 中也有父级

      &lt;style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"&gt;

      从两者中删除 .DarkActionBar 会有所帮助。然后,您可能可以为工具栏添加样式,如上述答案中所述。

      希望对你有帮助。

      【讨论】:

        【解决方案5】:

        这里的问题是我没有为工具栏应用主题。

        我通过在 xml 文件中的工具栏小部件中添加以下行来解决此问题:android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

        感谢大家的努力!

        【讨论】:

          猜你喜欢
          • 2022-08-12
          • 1970-01-01
          • 1970-01-01
          • 2017-11-16
          • 2019-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多