colorPrimary 和 colorPrimaryDark 在应用程序主题中被忽略
colorPrimary and colorPrimaryDark are ignored within application theme
(注意:这与 有点相关,所以可能有一些额外的信息被我不小心从这个问题中遗漏了)
我定义了以下主题:
<style name="Material" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/app_green</item>
<item name="colorPrimaryDark">@color/app_green_dark</item>
<item name="android:textColorPrimary">@color/action_bar_text</item>
<item name="android:textColor">@color/secondary_text_color</item>
<item name="android:color">@color/secondary_text_color</item>
<item name="colorAccent">@color/app_green</item>
<item name="android:editTextColor">@color/secondary_text_color</item>
<item name="textHeaderMaxLines">@integer/text_header_max_lines</item>
<item name="trackAbstractMaxLines">@integer/track_abstract_max_lines</item>
<item name="activatableItemBackground">@drawable/activatable_item_background</item>
<!-- ActionMode Styles -->
<item name="android:windowActionModeOverlay">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="actionModeStyle">@style/Material.Widget.ActionMode</item>
<!-- Global UI Assignments -->
<item name="android:spinnerStyle">@style/Material.Widget.Spinner</item>
<item name="android:buttonStyle">@style/Material.Widget.Button</item>
<item name="android:checkboxStyle">@style/Material.Widget.Checkbox</item>
<item name="android:textAppearance">@android:style/TextAppearance</item>
<item name="android:popupWindowStyle">@style/Material.Window.Popup</item>
<!-- ViewPager -->
<item name="vpiCirclePageIndicatorStyle">@style/Material.Activity.Login.ViewPagerIndicator.CustomCircle</item>
<item name="buttonBarStyle">?android:buttonBarStyle</item>
<item name="buttonBarButtonStyle">?android:buttonBarButtonStyle</item>
<item name="indeterminateProgressStyle">?android:indeterminateProgressStyle</item>
<!-- API 14+ (compatibility) -->
<item name="listPreferredItemPaddingLeft">@dimen/compat_list_preferred_item_padding_left</item>
<item name="listPreferredItemPaddingRight">@dimen/compat_list_preferred_item_padding_right</item>
<item name="listPreferredItemHeightSmall">@dimen/compat_list_preferred_item_height_small</item>
</style>
我使用这个主题如下:
<application
android:name=".MyApp"
android:icon="@drawable/icon"
android:logo="@null"
android:label="@string/app_name"
android:theme="@style/Material"
android:hardwareAccelerated="true"
android:allowBackup="true">
<activity
android:name=".ui.MainActivity"
android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.DetailActivity"
android:label="Details"
android:theme="@style/Material.Activity" >
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>
我所有的活动都来自 NavigationDrawerActivity
:
/**
* An {@link Activity} that supports a Navigation Drawer, which is a pull-out panel for navigation
* menus. This drawer is pulled out from the left side of the screen (right side on RTL devices).
*/
public class NavigationDrawerActivity extends ActionBarActivity
implements AdapterView.OnItemClickListener {
private static final String LOGTAG = NavigationDrawerActivity.class.getSimpleName();
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private LayoutInflater mInflater;
private NavigationDrawerItemAdapter mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private NavigationDrawerItem[] mNavigationDrawerItems;
private Toolbar mAppBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We have to call super.setContentView() here because BaseActivity redefines setContentView(),
// and we don't want to use that.
super.setContentView(R.layout.navigation_drawer);
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setupNavigationDrawer();
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
return mDrawerToggle.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
/**
* Toggles the state of the navigation drawer (i.e. closes it if it's open, and opens it if
* it's closed).
*/
public void toggleNavigationDrawer() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
closeNavigationDrawer();
} else {
openNavigationDrawer();
}
}
/**
* Opens the navigation drawer.
*/
public void openNavigationDrawer() {
mDrawerLayout.openDrawer(GravityCompat.START);
}
/**
* Closes the navigation drawer.
*/
public void closeNavigationDrawer() {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
/**
* Initializes items specific to the navigation drawer.
*/
private void setupNavigationDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.wiw_green));
mAppBar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(mAppBar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* Our context (Activity that hosts this drawer) */
mDrawerLayout, /* The DrawerLayout where the nav drawer will be drawn */
R.string.drawer_open, /* Description of "open drawer", for accessibility */
R.string.drawer_close /* Description of "close drawer", for accessibility */
) {
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
supportInvalidateOptionsMenu();
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
supportInvalidateOptionsMenu();
}
};
mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.drawer_list);
mNavigationDrawerItems = buildNavDrawerItemsList();
setupAdapter(mNavigationDrawerItems);
setupNavigationDrawerHeader();
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View aView, int aPosition, long aId) {
// Code not relevant
}
/**
* Set the inner content view of this {@link NavigationDrawerActivity} to have a given layout.
*
* @param aLayoutId The id of the layout to load into the inner content view of this activity.
*/
public void setDrawerContent(int aLayoutId) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup)findViewById(R.id.drawer_content);
inflater.inflate(aLayoutId, root);
}
}
在NavigationDrawerActivity
中,我实际上必须手动设置状态栏背景颜色(参见setupNavigationDrawer()
的第二行),而不是从colorPrimaryDark
自动设置Android 5.0 设备。此外,更改 colorPrimary
或 colorPrimaryDark
中的颜色 不会 更改状态栏的颜色(尽管这可能是 windowTranslucentStatus
设置为 true
) 或 Toolbar
背景颜色。
我想知道我能做些什么来缓解这个问题,因为我认为它在我的 Material 风格的主题中引起了其他问题。
您引用的文档是指原生 Android 5.0 Theme.Material
。 appcompat-v7
似乎不会自动将任何颜色应用到状态栏,至少目前是这样。
因此,例如 this sample project, when run on an Android 5.0 device, gets the status bar color, because it is using Theme.Material
. This port of the same sample project to use appcompat-v7
does not. This revised version of the same sample 使用 appcompat-v7
并专门请求状态栏颜色更改获得颜色。
(注意:这与
我定义了以下主题:
<style name="Material" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/app_green</item>
<item name="colorPrimaryDark">@color/app_green_dark</item>
<item name="android:textColorPrimary">@color/action_bar_text</item>
<item name="android:textColor">@color/secondary_text_color</item>
<item name="android:color">@color/secondary_text_color</item>
<item name="colorAccent">@color/app_green</item>
<item name="android:editTextColor">@color/secondary_text_color</item>
<item name="textHeaderMaxLines">@integer/text_header_max_lines</item>
<item name="trackAbstractMaxLines">@integer/track_abstract_max_lines</item>
<item name="activatableItemBackground">@drawable/activatable_item_background</item>
<!-- ActionMode Styles -->
<item name="android:windowActionModeOverlay">true</item>
<item name="windowActionModeOverlay">true</item>
<item name="actionModeStyle">@style/Material.Widget.ActionMode</item>
<!-- Global UI Assignments -->
<item name="android:spinnerStyle">@style/Material.Widget.Spinner</item>
<item name="android:buttonStyle">@style/Material.Widget.Button</item>
<item name="android:checkboxStyle">@style/Material.Widget.Checkbox</item>
<item name="android:textAppearance">@android:style/TextAppearance</item>
<item name="android:popupWindowStyle">@style/Material.Window.Popup</item>
<!-- ViewPager -->
<item name="vpiCirclePageIndicatorStyle">@style/Material.Activity.Login.ViewPagerIndicator.CustomCircle</item>
<item name="buttonBarStyle">?android:buttonBarStyle</item>
<item name="buttonBarButtonStyle">?android:buttonBarButtonStyle</item>
<item name="indeterminateProgressStyle">?android:indeterminateProgressStyle</item>
<!-- API 14+ (compatibility) -->
<item name="listPreferredItemPaddingLeft">@dimen/compat_list_preferred_item_padding_left</item>
<item name="listPreferredItemPaddingRight">@dimen/compat_list_preferred_item_padding_right</item>
<item name="listPreferredItemHeightSmall">@dimen/compat_list_preferred_item_height_small</item>
</style>
我使用这个主题如下:
<application
android:name=".MyApp"
android:icon="@drawable/icon"
android:logo="@null"
android:label="@string/app_name"
android:theme="@style/Material"
android:hardwareAccelerated="true"
android:allowBackup="true">
<activity
android:name=".ui.MainActivity"
android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.DetailActivity"
android:label="Details"
android:theme="@style/Material.Activity" >
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>
我所有的活动都来自 NavigationDrawerActivity
:
/**
* An {@link Activity} that supports a Navigation Drawer, which is a pull-out panel for navigation
* menus. This drawer is pulled out from the left side of the screen (right side on RTL devices).
*/
public class NavigationDrawerActivity extends ActionBarActivity
implements AdapterView.OnItemClickListener {
private static final String LOGTAG = NavigationDrawerActivity.class.getSimpleName();
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private LayoutInflater mInflater;
private NavigationDrawerItemAdapter mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private NavigationDrawerItem[] mNavigationDrawerItems;
private Toolbar mAppBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We have to call super.setContentView() here because BaseActivity redefines setContentView(),
// and we don't want to use that.
super.setContentView(R.layout.navigation_drawer);
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setupNavigationDrawer();
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
return mDrawerToggle.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
/**
* Toggles the state of the navigation drawer (i.e. closes it if it's open, and opens it if
* it's closed).
*/
public void toggleNavigationDrawer() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
closeNavigationDrawer();
} else {
openNavigationDrawer();
}
}
/**
* Opens the navigation drawer.
*/
public void openNavigationDrawer() {
mDrawerLayout.openDrawer(GravityCompat.START);
}
/**
* Closes the navigation drawer.
*/
public void closeNavigationDrawer() {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
/**
* Initializes items specific to the navigation drawer.
*/
private void setupNavigationDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.wiw_green));
mAppBar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(mAppBar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* Our context (Activity that hosts this drawer) */
mDrawerLayout, /* The DrawerLayout where the nav drawer will be drawn */
R.string.drawer_open, /* Description of "open drawer", for accessibility */
R.string.drawer_close /* Description of "close drawer", for accessibility */
) {
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
supportInvalidateOptionsMenu();
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
supportInvalidateOptionsMenu();
}
};
mDrawerList = (ListView) mDrawerLayout.findViewById(R.id.drawer_list);
mNavigationDrawerItems = buildNavDrawerItemsList();
setupAdapter(mNavigationDrawerItems);
setupNavigationDrawerHeader();
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View aView, int aPosition, long aId) {
// Code not relevant
}
/**
* Set the inner content view of this {@link NavigationDrawerActivity} to have a given layout.
*
* @param aLayoutId The id of the layout to load into the inner content view of this activity.
*/
public void setDrawerContent(int aLayoutId) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup)findViewById(R.id.drawer_content);
inflater.inflate(aLayoutId, root);
}
}
在NavigationDrawerActivity
中,我实际上必须手动设置状态栏背景颜色(参见setupNavigationDrawer()
的第二行),而不是从colorPrimaryDark
自动设置Android 5.0 设备。此外,更改 colorPrimary
或 colorPrimaryDark
中的颜色 不会 更改状态栏的颜色(尽管这可能是 windowTranslucentStatus
设置为 true
) 或 Toolbar
背景颜色。
我想知道我能做些什么来缓解这个问题,因为我认为它在我的 Material 风格的主题中引起了其他问题。
您引用的文档是指原生 Android 5.0 Theme.Material
。 appcompat-v7
似乎不会自动将任何颜色应用到状态栏,至少目前是这样。
因此,例如 this sample project, when run on an Android 5.0 device, gets the status bar color, because it is using Theme.Material
. This port of the same sample project to use appcompat-v7
does not. This revised version of the same sample 使用 appcompat-v7
并专门请求状态栏颜色更改获得颜色。