Reputation: 120324
I would like to position a view below an action bar with overlay. Is there a way of doing this via XML layout?
I would like to avoid using constants as there are already at least 4 possible values and that number is likely to grow.
Compatibility with ActionBarSherlock is a plus.
Upvotes: 19
Views: 12652
Reputation: 76075
android:layout_marginTop="?attr/actionBarSize"
This will account for changes in size based on screen configuration automatically. You can see a demo of its use in the "Overlay" example of the 'Demos' sample that comes with ActionBarSherlock.
Upvotes: 46
Reputation: 120324
Jake Wharton's answer does not work if you're handling configuration changes.
This is how I solved this problem by code, in case it helps anyone:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
layoutTheView();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
layoutTheView();
}
private void layoutTheView() {
ActionBar actionBar = this.getSupportActionBar();
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mTheView.getLayoutParams();
int actionBarHeight = actionBar.getHeight();
params.setMargins(0, actionBarHeight, 0, 0);
mTheView.setLayoutParams(params);
mTheView.requestLayout();
}
Upvotes: 7