众所周知,sdk 级别 11 允许操作栏的彩色背景,观看 this .但是有一些带有彩色操作栏的应用程序的最小 sdk 低于 11。例如,Whatsapp 具有绿色操作栏但具有 min sdk:7 WhatsApp FAQ ,或者 Telegram 应用程序有 min sdk:8 Telegram FAQ , 但有蓝色操作栏。
这些应用程序如何工作?我该如何做到这一点?
android.app.ActionBar actionBar = (android.app.ActionBar) getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
View mActionBarView = getLayoutInflater().inflate(R.layout.action_bar_main, null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
对于 getActionBar()
,此代码需要 minSdk = 11。请帮助我。
最佳答案
Toolbar
(Lollipop 中引入的 ActionBar 的新名称)只是一个普通的 View 。
链接:https://developer.android.com/reference/android/support/v7/widget/Toolbar.html
您只需像设置任何其他 View 一样设置它的背景。
// java
toolbar.setBackgroundColor(int color);
// or
toolbar.setBackgroundResource(int resId);
// or XML
android:background="@drawable/toolbarBackground"
关于android - 如何在不增加 min sdk 的情况下更改操作栏的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38686186/