zna*_*nat 179 android android-actionbar
我ActionBar每次创建一个活动时都试图获得(使用Sherlock)的高度(特别是在ActionBar高度可能改变的情况下处理轮换时的配置更改).
为此,我使用的方法ActionBar.getHeight()仅在ActionBar显示时才有效.
当第一次创建的第一个活动,我可以调用getHeight()的onCreateOptionsMenu回调.但是这个方法之后没有被调用.
所以我的问题是我什么时候可以调用getHeight()并确保它不会返回0?或者如果不可能,我如何设置ActionBar的高度?
Ant*_*ony 406
虽然@ birdy的答案是一个选项,如果你想显式控制ActionBar大小,有一种方法可以在不锁定我在支持文档中找到的大小的情况下提取它.这有点尴尬,但它对我有用.您需要一个上下文,此示例在Activity中有效.
// Calculate ActionBar height
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
Run Code Online (Sandbox Code Playgroud)
ces*_*rds 143
在XML中,您应该使用此属性:
android:paddingTop="?android:attr/actionBarSize"
Run Code Online (Sandbox Code Playgroud)
laa*_*ptu 16
@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());
}
Run Code Online (Sandbox Code Playgroud)
hri*_*.to 16
好吧,我正在谷歌上搜索并多次访问这个帖子,所以我认为不仅对Sherlock而且对appcompat也有好处:
使用appCompat操作栏高度
与@Anthony描述相似,您可以使用以下代码找到它(我刚刚更改了资源ID):
TypedValue tv = new TypedValue();
if (getActivity().getTheme().resolveAttribute(R.attr.actionBarSize, tv, true))
{
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
Run Code Online (Sandbox Code Playgroud)
预Sandwitch问题
我需要操作栏高度,因为在ICE_CREAM_SANDWITCH设备之前,我的视图是重叠操作栏.我尝试设置android:layout_marginTop ="?android:attr/actionBarSize",android:layout_marginTop ="?attr/actionBarSize",将重叠设置为视图/操作栏,甚至修改了自定义主题的动作栏高度,但没有任何效果.这就是它的样子:

不幸的是我发现用上面描述的方法我得到的高度只有一半(我认为选项栏没有采取到位)所以要完全修复我需要双倍动作栏高度,一切似乎都可以:

如果有人知道更好的解决方案(而不是加倍actionBarHeight)我会很高兴让我知道,因为我来自iOS开发,我发现大多数Android视图的东西很混乱:)
问候,
hris.to
and*_*per 13
我认为最安全的方式是:
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;
}
Run Code Online (Sandbox Code Playgroud)
要设置高度,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>
Run Code Online (Sandbox Code Playgroud)
并将此设置Theme为Activity:
android:theme="@style/Theme.FixedSize"
Run Code Online (Sandbox Code Playgroud)
爪哇:
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);
Run Code Online (Sandbox Code Playgroud)
xml:
?attr/actionBarSize
Run Code Online (Sandbox Code Playgroud)
如果您将工具栏用作ActionBar,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud)
然后只需使用工具栏的layout_height。
int actionBarHeight = toolbar.getLayoutParams().height;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
161957 次 |
| 最近记录: |