我想获得的高度ActionBar
(使用福尔摩斯)的每一项活动,创建时间(特别处理的轮播,动作条的高度可能会改变配置更改)。
为此,我使用的方法ActionBar.getHeight()
的时只能ActionBar
中示出。
当第一次创建的第一个活动,我可以调用getHeight()
在onCreateOptionsMenu
回调。 但这种方法不叫。
所以我的问题是我何时可以调用的getHeight()和放心,它不会返回0? 或者如果它是不可能的,我怎么能设置动作条的高度?
Answer 1:
而如果你想明确地控制动作条大小@小鸟的回答是一个选项,有一个办法把它拉起来不锁定,我支持文档中找到的大小。 这是一个有点尴尬,但它为我工作。 你需要一个背景下,这个例子是有效的一个活动。
// Calculate ActionBar height
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
Answer 2:
在XML中,你应该使用这个属性:
android:paddingTop="?android:attr/actionBarSize"
Answer 3:
好吧,我在谷歌上搜索周围,并获得这个职位几次,所以我认为这将是很好的不仅是福尔摩斯但程序兼容性也被描述:
使用程序兼容性操作栏高度
漂亮类同@Anthony描述,你可以用下面的代码(我只是改变资源ID)找到它:
TypedValue tv = new TypedValue();
if (getActivity().getTheme().resolveAttribute(R.attr.actionBarSize, tv, true))
{
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
预Sandwitch问题
我需要操作栏高度,因为在预ICE_CREAM_SANDWITCH设备我的看法是overlaping行动吧。 我尝试设置机器人:layout_marginTop =“机器人:ATTR / actionBarSize”,机器人“?ATTR / actionBarSize” layout_marginTop =,设置重叠关闭查看/动作条,甚至固定的动作条的高度自定义的主题,但毫无效果。 这就是它的样子:
不幸的是我发现与方法上面描述我只得到了高度的一半(我假设选项栏中不到位所),所以才能彻底解决我需要操作栏高度两倍的isue,一切似乎确定:
如果有人知道更好的解决方案(增加了一倍actionBarHeight)我会很高兴,让我知道,因为我来自iOS开发,我发现大多数的Android设备上查看的东西非常令人迷惑:)
问候,
hris.to
Answer 4:
@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());
}
Answer 5:
我认为最安全的办法是:
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;
}
Answer 6:
要设置的高度, 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>
并将此Theme
给您的Activity
:
android:theme="@style/Theme.FixedSize"
Answer 7:
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:
?attr/actionBarSize
Answer 8:
如果您使用的是工具栏的动作条,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
然后只需使用工具栏的layout_height。
int actionBarHeight = toolbar.getLayoutParams().height;
Answer 9:
您可以使用此功能来获得ActionBar的高度。
public int getActionBarHeight() {
final TypedArray ta = getContext().getTheme().obtainStyledAttributes(
new int[] {android.R.attr.actionBarSize});
int actionBarHeight = (int) ta.getDimension(0, 0);
return actionBarHeight;
}
Answer 10:
这个辅助方法应该派上用场的人。 例如: int actionBarSize = getThemeAttributeDimensionSize(this, android.R.attr.actionBarSize);
public static int getThemeAttributeDimensionSize(Context context, int attr)
{
TypedArray a = null;
try{
a = context.getTheme().obtainStyledAttributes(new int[] { attr });
return a.getDimensionPixelSize(0, 0);
}finally{
if(a != null){
a.recycle();
}
}
}
Answer 11:
你只需要添加此。
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;
}
Answer 12:
我发现了一个更通用的方式来发现它:
int[] location = new int[2];
mainView.getLocationOnScreen(location);
int toolbarHeight = location[1];
其中“MAINVIEW”是布局的根视图。
这个想法基本上得到MAINVIEW的Y位置,因为它是设置正下方的动作条(或工具栏)。
Answer 13:
操作栏高度根据所施加的主题不同。 如果您使用AppCompatActivity高度将是从正常的活动在大多数情况下是不同的。
如果您使用AppCompatActivity你应该使用R.attr.actionBarSize代替android.R.attr.actionBarSize
public static int getActionBarHeight(Activity activity) {
TypedValue typedValue = new TypedValue();
int attributeResourceId = android.R.attr.actionBarSize;
if (activity instanceof AppCompatActivity) {
attributeResourceId = R.attr.actionBarSize;
}
if (activity.getTheme().resolveAttribute(attributeResourceId, typedValue, true)) {
return TypedValue.complexToDimensionPixelSize(typedValue.data, activity.getResources().getDisplayMetrics());
}
return (int) Math.floor(activity.getResources()
.getDimension(R.dimen.my_default_value));
}
Answer 14:
在XML中,你可以使用ATTR / actionBarSize,但如果你需要访问在Java中,你需要使用下面的代码值?
public int getActionBarHeight() {
int actionBarHeight = 0;
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 {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
getResources().getDisplayMetrics());
}
return actionBarHeight;
}
Answer 15:
如果您使用Xamarin / C#,这是你要寻找的代码:
var styledAttributes = Context.Theme.ObtainStyledAttributes(new[] { Android.Resource.Attribute.ActionBarSize });
var actionBarSize = (int)styledAttributes.GetDimension(0, 0);
styledAttributes.Recycle();
Answer 16:
The action bar now enhanced to app bar.So you have to add conditional check for getting height of action bar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
height = getActivity().getActionBar().getHeight();
} else {
height = ((ActionBarActivity) getActivity()).getSupportActionBar().getHeight();
}
Answer 17:
一个现成的方法来满足最普遍的回答:
public static int getActionBarHeight(
Activity activity) {
int actionBarHeight = 0;
TypedValue typedValue = new TypedValue();
try {
if (activity
.getTheme()
.resolveAttribute(
android.R.attr.actionBarSize,
typedValue,
true)) {
actionBarHeight =
TypedValue.complexToDimensionPixelSize(
typedValue.data,
activity
.getResources()
.getDisplayMetrics());
}
} catch (Exception ignore) {
}
return actionBarHeight;
}
Answer 18:
一个简单的方法来找到动作条的高度是从活动onPrepareOptionMenu
方法。
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
....
int actionBarHeight = getActionBar().getHeight());
.....
}
Answer 19:
在JavaFX(使用胶子),高度在运行时设置或类似的东西。 虽然能够得到宽度就像正常的...
double width = appBar.getWidth();
你必须创建一个监听器:
GluonApplication application = this;
application.getAppBar().heightProperty().addListener(new ChangeListener(){
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
// Take most recent value given here
application.getAppBar.heightProperty.get()
}
});
...而采取的最新值改成了。 这个高度。
Answer 20:
尝试一切,是在那里没有成功后,我发现,意外,功能也很简单的方法来操作栏的默认高度。
只有在API 25和24测试
C#
Resources.GetDimensionPixelSize(Resource.Dimension.abc_action_bar_default_height_material);
Java的
getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height_material);
文章来源: How to get the ActionBar height?