Android 如何获取 ActionBar 的高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12301510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the ActionBar height?
提问by znat
I am trying to get the height of the ActionBar
(using Sherlock) every time an activity is created (specially to handle configuration changes on rotation where the ActionBar height might change).
ActionBar
每次创建活动时,我都试图获取(使用 Sherlock)的高度(特别是为了处理 ActionBar 高度可能发生变化的旋转配置更改)。
For this I use the method ActionBar.getHeight()
which works only when the ActionBar
is shown.
为此,我使用ActionBar.getHeight()
仅在ActionBar
显示时才有效的方法。
When the first activity is created for the first time, I can call getHeight()
in the onCreateOptionsMenu
callback. But this method is not called after.
当第一次创建的第一个活动,我可以调用getHeight()
的onCreateOptionsMenu
回调。但是这个方法没有被调用。
So my question is when can I call getHeight() and be assured that it doesn't return 0? Or if it is not possible, how can I set the height of the ActionBar ?
所以我的问题是我什么时候可以调用 getHeight() 并确保它不会返回 0?或者,如果不可能,我该如何设置 ActionBar 的高度?
回答by Anthony
While @birdy's answer is an option if you want to explicitly control the ActionBar size, there is a way to pull it up without locking the size that I found in support documentation. It's a little awkward but it's worked for me. You'll need a context, this example would be valid in an Activity.
如果您想明确控制 ActionBar 的大小,@birdy 的答案是一个选项,但有一种方法可以在不锁定我在支持文档中找到的大小的情况下将其拉起。这有点尴尬,但对我有用。你需要一个上下文,这个例子在一个活动中是有效的。
// Calculate ActionBar height
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
回答by cesards
In XML, you should use this attribute:
在 XML 中,您应该使用此属性:
android:paddingTop="?android:attr/actionBarSize"
回答by hris.to
Ok I was googling around and get to this post several times so I think it'll be good to be described not only for Sherlock but for appcompat also:
好吧,我在谷歌上搜索并多次访问这篇文章,所以我认为不仅对 Sherlock 进行描述,而且对 appcompat 也进行描述会很好:
Action Bar height using appCompat
使用 appCompat 的操作栏高度
Pretty similiar to @Anthony description you could find it with following code(I've just changed resource id):
与@Anthony 描述非常相似,您可以使用以下代码找到它(我刚刚更改了资源 ID):
TypedValue tv = new TypedValue();
if (getActivity().getTheme().resolveAttribute(R.attr.actionBarSize, tv, true))
{
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
Pre Sandwitch Problem
预三明治问题
I needed action bar height because on pre ICE_CREAM_SANDWITCH devices my view was overlaping action bar. I tried setting android:layout_marginTop="?android:attr/actionBarSize", android:layout_marginTop="?attr/actionBarSize", setting overlap off to view/actionbar and even fixed actionbar height with custom theme but nothing worked. That's how it looked:
我需要操作栏高度,因为在 ICE_CREAM_SANDWITCH 设备之前,我的视图与操作栏重叠。我尝试设置 android:layout_marginTop="?android:attr/actionBarSize", android:layout_marginTop="?attr/actionBarSize",将重叠设置为视图/操作栏,甚至使用自定义主题固定操作栏高度,但没有任何效果。那是它的样子:
Unfortunately I found out that with method described above I get only half of the height(I assume that options bar is not taken in place) so to completely fix the isue I need to doubleaction bar height and everything seems ok:
不幸的是,我发现使用上面描述的方法我只得到了一半的高度(我假设选项栏没有到位)所以要完全修复问题,我需要将动作栏高度加倍,一切似乎都没问题:
If someone knows better solution(than doubling actionBarHeight) I'll be glad to let me know as I come from iOS development and I found most of Android view's stuff pretty confusing :)
如果有人知道更好的解决方案(而不是将 actionBarHeight 加倍),我很高兴告诉我,因为我来自 iOS 开发,我发现大多数 Android 视图的东西都非常令人困惑:)
Regards,
问候,
hris.to
克里斯托
回答by laaptu
@Anthony answer works for devices that support ActionBar
and for those devices which support only Sherlock Action Bar
following method must be used
@Anthony 答案适用于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());
}
回答by android developer
i think the safest way would be :
我认为最安全的方法是:
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;
}
回答by birdy
To set the height of ActionBar
you can create new Theme
like this one:
要设置高度,ActionBar
您可以Theme
像这样创建新的:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.FixedSize" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarSize">48dip</item>
<item name="android:actionBarSize">48dip</item>
</style>
</resources>
and set this Theme
to your Activity
:
并将其设置Theme
为您的Activity
:
android:theme="@style/Theme.FixedSize"
回答by TeeTracker
Java:
爪哇:
int actionBarHeight;
int[] abSzAttr;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
abSzAttr = new int[] { android.R.attr.actionBarSize };
} else {
abSzAttr = new int[] { R.attr.actionBarSize };
}
TypedArray a = obtainStyledAttributes(abSzAttr);
actionBarHeight = a.getDimensionPixelSize(0, -1);
xml:
xml:
?attr/actionBarSize
回答by jk7
If you are using a Toolbar as the ActionBar,
如果您使用工具栏作为操作栏,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
then just use the layout_height of the toolbar.
然后只需使用工具栏的 layout_height 。
int actionBarHeight = toolbar.getLayoutParams().height;
回答by wangzhengyi
you can use this function to get actionbar's height.
您可以使用此功能来获取操作栏的高度。
public int getActionBarHeight() {
final TypedArray ta = getContext().getTheme().obtainStyledAttributes(
new int[] {android.R.attr.actionBarSize});
int actionBarHeight = (int) ta.getDimension(0, 0);
return actionBarHeight;
}
回答by JCDecary
you just have to add this.
你只需要添加这个。
public int getActionBarHeight() {
int height;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
height = getActivity().getActionBar().getHeight();
} else {
height = ((ActionBarActivity) getActivity()).getSupportActionBar().getHeight();
}
return height;
}