我最近在 Jelly Bean 设备上测试了我的应用程序,发现我的 Actionbar Dodging Code 不再有效。
我有一个 OverlayMode 为 true 的透明 Actionbar,但我想在我的某些屏幕中将 Actionbar 表现得像实心 actionbar。
为了使它正常工作,我从 Honeycomb Gallery 借用了一些代码 Code
基本上我检查 Acionbar 高度并将 android.R.id.content 桶的 topMargin 设置为此值。
public void setupActionbar() {
final int sdkVersion = Build.VERSION.SDK_INT;
int barHeight = getSupportActionBar().getHeight();
if (sdkVersion < Build.VERSION_CODES.HONEYCOMB) {
FrameLayout content = (FrameLayout) findViewById(android.R.id.content);
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) content.getLayoutParams();
if (params.topMargin != barHeight) {
params.topMargin = barHeight;
content.setLayoutParams(params);
}
if (!getSupportActionBar().isShowing()) {
params.topMargin = 0;
content.setLayoutParams(params);
}
} else {
FrameLayout content = (FrameLayout) findViewById(android.R.id.content);
LayoutParams params = content.getLayoutParams();
if (params instanceof RelativeLayout.LayoutParams) {
android.widget.RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) params;
if (lp.topMargin != barHeight) {
lp.topMargin = barHeight;
content.setLayoutParams(lp);
}
if (!getActionBar().isShowing()) {
lp.topMargin = 0;
content.setLayoutParams(lp);
}
} else if (params instanceof FrameLayout.LayoutParams) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params;
if (lp.topMargin != barHeight) {
lp.topMargin = barHeight;
content.setLayoutParams(params);
}
if (!getActionBar().isShowing()) {
lp.topMargin = 0;
content.setLayoutParams(params);
}
}
}
在 Jelly Beans 中,由于某种原因,这种策略不再有效。 Jelly Bean 是否更改了 id.content 容器?
最佳答案
好的,在这里回答我自己的问题。 首先,我的代码中有一个拼写错误:
content.setLayoutParams(params); should read
content.setLayoutParams(lp);
但真正的问题是
FrameLayout content = (FrameLayout) findViewById(android.R.id.content);
提供整个屏幕的 View ,包括状态栏。 仅适用于 Android 4.1 Jelly Bean
View content = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
给出需要在Transparent Actionbar下推送的RootContentView。
关于android - Jelly Bean android.R.id.content 改变了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11665677/