可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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).
For this I use the method ActionBar.getHeight()
which works only when the ActionBar
is shown.
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.
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 ?
回答1:
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.
// Calculate ActionBar height
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
回答2:
In XML, you should use this attribute:
android:paddingTop=\"?android:attr/actionBarSize\"
回答3:
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:
Action Bar height using appCompat
Pretty similiar to @Anthony description you could find it with following code(I\'ve just changed resource 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:
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 double action 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 :)
Regards,
hris.to
回答4:
@Anthony answer works for devices that support ActionBar
and for those devices which support only Sherlock Action Bar
following method must be used
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());
}
回答5:
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;
}
回答6:
To set the height of ActionBar
you can create new Theme
like this one:
<?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
:
android:theme=\"@style/Theme.FixedSize\"
回答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
回答8:
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.
int actionBarHeight = toolbar.getLayoutParams().height;
回答9:
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;
}
回答10:
This helper method should come in handy for someone. Example: 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();
}
}
}
回答11:
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;
}
回答12:
I found a more generic way to discover it:
int[] location = new int[2];
mainView.getLocationOnScreen(location);
int toolbarHeight = location[1];
where \'mainView\' is the root view of your layout.
The idea is basically get the Y position of the mainView as it is set right below the ActionBar (or Toolbar).
回答13:
The action bar height differs according to the applied theme.
If you\'re using AppCompatActivity the height will be different in most of cases from the normal Activity.
If you\'re using AppCompatActivity you should use R.attr.actionBarSize instead of 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));
}
回答14:
In xml, you can use ?attr/actionBarSize, but if you need access to that value in Java you need to use below code:
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;
}
回答15:
If you\'re using Xamarin/C#, this is the code you\'re looking for:
var styledAttributes = Context.Theme.ObtainStyledAttributes(new[] { Android.Resource.Attribute.ActionBarSize });
var actionBarSize = (int)styledAttributes.GetDimension(0, 0);
styledAttributes.Recycle();
回答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();
}
回答17:
A ready method to fulfill the most popular answer:
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;
}
回答18:
A simple way to find actionbar height is from Activity onPrepareOptionMenu
method.
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
....
int actionBarHeight = getActionBar().getHeight());
.....
}
回答19:
In javafx (using Gluon), the height is set at runtime or something like it. While being able to get the width just like normal...
double width = appBar.getWidth();
You have to create a listener:
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()
}
});
...And take the most recent value it changed to. To get the height.
回答20:
After trying everything that\'s out there without success, I found out, by accident, a functional and very easy way to get the action bar\'s default height.
Only tested in API 25 and 24
C#
Resources.GetDimensionPixelSize(Resource.Dimension.abc_action_bar_default_height_material);
Java
getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height_material);