[英]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.