【发布时间】:2015-07-06 14:34:04
【问题描述】:
我已经对这个主题进行了一些研究,但我找不到完整的解决方案,因此,我一步一步地尝试和错误,我终于找到了如何实现这些结果:透明或彩色Actionbar 和 Statusbar。请参阅下面的答案。
【问题讨论】:
标签: android android-actionbar android-toolbar android-appcompat android-statusbar
我已经对这个主题进行了一些研究,但我找不到完整的解决方案,因此,我一步一步地尝试和错误,我终于找到了如何实现这些结果:透明或彩色Actionbar 和 Statusbar。请参阅下面的答案。
【问题讨论】:
标签: android android-actionbar android-toolbar android-appcompat android-statusbar
在操作栏和状态栏自定义方面,我正在开发一个应用程序,该应用程序需要在具有 >= API14 的所有设备中看起来相似。我终于找到了一个解决方案,由于我花了一些时间,我会分享它以节省你的一些。 我们首先使用 appcompat-21 依赖项。
透明操作栏:
values/styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light">
...
</style>
<style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
<item name="android:windowContentOverlay">@null</item>
<item name="windowActionBarOverlay">true</item>
<item name="colorPrimary">@android:color/transparent</item>
</style>
<style name="AppTheme.ActionBar" parent="AppTheme">
<item name="windowActionBarOverlay">false</item>
<item name="colorPrimary">@color/default_yellow</item>
</style>
values-v21/styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light">
...
</style>
<style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
<item name="colorPrimary">@android:color/transparent</item>
</style>
<style name="AppTheme.ActionBar" parent="AppTheme">
<item name="colorPrimaryDark">@color/bg_colorPrimaryDark</item>
<item name="colorPrimary">@color/default_yellow</item>
</style>
现在您可以在AndroidManifest.xml 中使用这些主题来指定哪些活动将具有透明或彩色ActionBar:
<activity
android:name=".MyTransparentActionbarActivity"
android:theme="@style/AppTheme.ActionBar.Transparent"/>
<activity
android:name=".MyColoredActionbarActivity"
android:theme="@style/AppTheme.ActionBar"/>
注意:在 API>=21 中,要获得 Actionbar 透明,您还需要获得 Statusbar 透明,否则将不尊重您的颜色样式并保持浅灰色。
透明状态栏(仅适用于 API>=19):
这很简单,只需使用以下代码:
protected void setStatusBarTranslucent(boolean makeTranslucent) {
if (makeTranslucent) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
但你会注意到一个奇怪的结果:
这是因为当Statusbar 透明时,布局将使用它的高度。为了防止这种情况,我们只需要:
解决方案一:
将此行 android:fitsSystemWindows="true" 添加到您想要放置在操作栏下方的任何内容的布局视图容器中:
...
<LinearLayout
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</LinearLayout>
...
解决方案二:
在我们之前的方法中添加几行:
protected void setStatusBarTranslucent(boolean makeTranslucent) {
View v = findViewById(R.id.bellow_actionbar);
if (v != null) {
int paddingTop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? MyScreenUtils.getStatusBarHeight(this) : 0;
TypedValue tv = new TypedValue();
getTheme().resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, tv, true);
paddingTop += TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
v.setPadding(0, makeTranslucent ? paddingTop : 0, 0, 0);
}
if (makeTranslucent) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
R.id.bellow_actionbar 将是我们想要放置在 Actionbar 下方的任何内容的布局容器视图 ID:
...
<LinearLayout
android:id="@+id/bellow_actionbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</LinearLayout>
...
就是这样,它认为我没有忘记什么。
在这个例子中,我没有使用Toolbar,但我认为它会得到相同的结果。这就是我自定义 Actionbar 的方式:
@Override
protected void onCreate(Bundle savedInstanceState) {
View vg = getActionBarView();
getWindow().requestFeature(vg != null ? Window.FEATURE_ACTION_BAR : Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(getContentView());
if (vg != null) {
getSupportActionBar().setCustomView(vg, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayUseLogoEnabled(false);
}
setStatusBarTranslucent(true);
}
注意:这是一个扩展 ActionBarActivity 的 abstract class
希望对您有所帮助!
【讨论】:
MyScreenUtils.getStatusBarHeight(this)这里是答案stackoverflow.com/a/3410200/1307690
FLAG_LAYOUT_NO_LIMITS标志,在getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);之后加上代码getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
在 KITKAT 之后支持。只需在 Activity 的 onCreate 方法中添加以下代码。无需对 Manifest 文件进行任何修改。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow(); // in Activity's onCreate() for instance
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
【讨论】:
只需将这些代码行添加到您的活动/片段 java 文件中:
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
);
【讨论】: