每次创建活动(特别是为了处理ActionBar高度可能发生变化的轮换时的配置更改)时,我都会尝试获取ActionBar
的高度(使用Sherlock)。
为此,我使用了ActionBar.getHeight()
方法,该方法仅在显示ActionBar
时有效。
当第一次创建第一个活动时,我可以在onCreateOptionsMenu
回调中调用getHeight()
。但此方法不是在之后调用的。
所以我的问题是什么时候我可以调用getHeight()并且确保它不会返回0?或者,如果不可能,我如何设置ActionBar的高度?
发布于 2014-02-18 03:54:37
在XML中,您应该使用以下属性:
android:paddingTop="?android:attr/actionBarSize"
发布于 2013-03-18 20:23:48
@Anthony answer适用于支持ActionBar
的设备和只支持Sherlock Action Bar
的设备,必须使用以下方法
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
if(actionBarHeight ==0 && getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true)){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
//OR as stated by @Marina.Eariel
TypedValue tv = new TypedValue();
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}else if(getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
发布于 2013-08-25 17:59:12
我认为最安全的方法是:
private int getActionBarHeight() {
int actionBarHeight = getSupportActionBar().getHeight();
if (actionBarHeight != 0)
return actionBarHeight;
final TypedValue tv = new TypedValue();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
} else if (getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
return actionBarHeight;
}
https://stackoverflow.com/questions/12301510
复制相似问题