hpique
hpique

Reputation: 120324

Position view below action bar with overlay

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

Answers (3)

Jake Wharton
Jake Wharton

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

Hitesh
Hitesh

Reputation: 66

use padding top

android:paddingTop="50dp"

Upvotes: -1

hpique
hpique

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

Related Questions