ActionBarContextView.java revision 269b8bb0cbd78969450ea9ab255232d047580210
189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell/*
289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * Copyright (C) 2010 The Android Open Source Project
389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell *
489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * you may not use this file except in compliance with the License.
689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * You may obtain a copy of the License at
789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell *
889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell *
1089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * Unless required by applicable law or agreed to in writing, software
1189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
1289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * See the License for the specific language governing permissions and
1489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * limitations under the License.
1589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell */
1689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellpackage com.android.internal.widget;
1789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
1889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport com.android.internal.R;
192c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powellimport com.android.internal.view.menu.ActionMenuView;
202c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powellimport com.android.internal.view.menu.MenuBuilder;
2189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
2289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.app.ActionBar;
2389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.content.Context;
2489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.content.res.TypedArray;
2589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.graphics.drawable.Drawable;
2689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.util.AttributeSet;
2789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.view.LayoutInflater;
2889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.view.View;
2989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.view.ViewGroup;
3089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.widget.ImageButton;
310e94b5151d817e600a888448a662208b29b5ef46Adam Powellimport android.widget.LinearLayout;
3289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellimport android.widget.TextView;
3389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
3489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell/**
3589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell * @hide
3689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell */
3789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powellpublic class ActionBarContextView extends ViewGroup {
3889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    // TODO: This must be defined in the default theme
3989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private static final int CONTENT_HEIGHT_DIP = 50;
4089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
4189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int mItemPadding;
4289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int mItemMargin;
432c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell    private int mActionSpacing;
4489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int mContentHeight;
4589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
4689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private CharSequence mTitle;
4789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private CharSequence mSubtitle;
4889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
4989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private ImageButton mCloseButton;
5089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private View mCustomView;
510e94b5151d817e600a888448a662208b29b5ef46Adam Powell    private LinearLayout mTitleLayout;
5289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private TextView mTitleView;
530e94b5151d817e600a888448a662208b29b5ef46Adam Powell    private TextView mSubtitleView;
5489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private Drawable mCloseDrawable;
55b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell    private ActionMenuView mMenuView;
5689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
5789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public ActionBarContextView(Context context) {
5889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        this(context, null, 0);
5989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
6089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
6189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public ActionBarContextView(Context context, AttributeSet attrs) {
6289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        this(context, attrs, 0);
6389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
6489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
6589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public ActionBarContextView(Context context, AttributeSet attrs, int defStyle) {
6689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        super(context, attrs, defStyle);
6789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
6889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        TypedArray a = context.obtainStyledAttributes(attrs,
6989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                com.android.internal.R.styleable.Theme);
7089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mItemPadding = a.getDimensionPixelOffset(
7189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                com.android.internal.R.styleable.Theme_actionButtonPadding, 0);
7289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        setBackgroundDrawable(a.getDrawable(
7389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                com.android.internal.R.styleable.Theme_actionBarContextBackground));
7489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mCloseDrawable = a.getDrawable(
7589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                com.android.internal.R.styleable.Theme_actionBarCloseContextDrawable);
7689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mItemMargin = mItemPadding / 2;
77dcdefbbff2bfecbfeb7b6459de130f376595c590Adam Powell
78dcdefbbff2bfecbfeb7b6459de130f376595c590Adam Powell        mContentHeight =
79dcdefbbff2bfecbfeb7b6459de130f376595c590Adam Powell                (int) (CONTENT_HEIGHT_DIP * getResources().getDisplayMetrics().density + 0.5f);
8089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        a.recycle();
8189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
8289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
8389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public void setCustomView(View view) {
8489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCustomView != null) {
8589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            removeView(mCustomView);
8689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
8789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mCustomView = view;
880e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout != null) {
890e94b5151d817e600a888448a662208b29b5ef46Adam Powell            removeView(mTitleLayout);
900e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mTitleLayout = null;
9189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
9289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (view != null) {
9389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            addView(view);
9489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
9589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        requestLayout();
9689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
970e94b5151d817e600a888448a662208b29b5ef46Adam Powell
9889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public void setTitle(CharSequence title) {
9989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mTitle = title;
1000e94b5151d817e600a888448a662208b29b5ef46Adam Powell        initTitle();
1010e94b5151d817e600a888448a662208b29b5ef46Adam Powell    }
1020e94b5151d817e600a888448a662208b29b5ef46Adam Powell
1030e94b5151d817e600a888448a662208b29b5ef46Adam Powell    public void setSubtitle(CharSequence subtitle) {
1040e94b5151d817e600a888448a662208b29b5ef46Adam Powell        mSubtitle = subtitle;
1050e94b5151d817e600a888448a662208b29b5ef46Adam Powell        initTitle();
1060e94b5151d817e600a888448a662208b29b5ef46Adam Powell    }
1070e94b5151d817e600a888448a662208b29b5ef46Adam Powell
10829ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    public CharSequence getTitle() {
10929ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell        return mTitle;
11029ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    }
11129ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell
11229ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    public CharSequence getSubtitle() {
11329ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell        return mSubtitle;
11429ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell    }
11529ed7575c2129974a57ad77b3531bc5995940c6cAdam Powell
1160e94b5151d817e600a888448a662208b29b5ef46Adam Powell    private void initTitle() {
1170e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout == null) {
11889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            LayoutInflater inflater = LayoutInflater.from(getContext());
1190e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mTitleLayout = (LinearLayout) inflater.inflate(R.layout.action_bar_title_item, null);
1200e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title);
1210e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle);
1220e94b5151d817e600a888448a662208b29b5ef46Adam Powell            if (mTitle != null) {
1230e94b5151d817e600a888448a662208b29b5ef46Adam Powell                mTitleView.setText(mTitle);
1240e94b5151d817e600a888448a662208b29b5ef46Adam Powell            }
1250e94b5151d817e600a888448a662208b29b5ef46Adam Powell            if (mSubtitle != null) {
1260e94b5151d817e600a888448a662208b29b5ef46Adam Powell                mSubtitleView.setText(mSubtitle);
12789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            }
1280e94b5151d817e600a888448a662208b29b5ef46Adam Powell            addView(mTitleLayout);
12989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        } else {
1300e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mTitleView.setText(mTitle);
1310e94b5151d817e600a888448a662208b29b5ef46Adam Powell            mSubtitleView.setText(mSubtitle);
1320e94b5151d817e600a888448a662208b29b5ef46Adam Powell            if (mTitleLayout.getParent() == null) {
1330e94b5151d817e600a888448a662208b29b5ef46Adam Powell                addView(mTitleLayout);
13489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            }
13589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
13689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
1370e94b5151d817e600a888448a662208b29b5ef46Adam Powell
13889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public void initForMode(final ActionBar.ContextMode mode) {
13989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCloseButton == null) {
14089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            mCloseButton = new ImageButton(getContext());
14189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            mCloseButton.setImageDrawable(mCloseDrawable);
14289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            mCloseButton.setBackgroundDrawable(null);
14389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
144269b8bb0cbd78969450ea9ab255232d047580210Adam Powell        mCloseButton.setOnClickListener(new OnClickListener() {
145269b8bb0cbd78969450ea9ab255232d047580210Adam Powell            public void onClick(View v) {
146269b8bb0cbd78969450ea9ab255232d047580210Adam Powell                mode.finish();
147269b8bb0cbd78969450ea9ab255232d047580210Adam Powell            }
148269b8bb0cbd78969450ea9ab255232d047580210Adam Powell        });
14989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        addView(mCloseButton);
15089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
1512c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        final MenuBuilder menu = (MenuBuilder) mode.getMenu();
152b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell        mMenuView = (ActionMenuView) menu.getMenuView(MenuBuilder.TYPE_ACTION_BUTTON, this);
153b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell        mMenuView.setOverflowReserved(true);
154b366bbae2b5a3009893ef64246e3430cea4b7736Adam Powell        mMenuView.updateChildren(false);
1552c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        addView(mMenuView);
15689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
1570e94b5151d817e600a888448a662208b29b5ef46Adam Powell
15889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    public void closeMode() {
15989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        removeAllViews();
16089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        mCustomView = null;
1612c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        mMenuView = null;
16289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
1630e94b5151d817e600a888448a662208b29b5ef46Adam Powell
16489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    @Override
165a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell    protected LayoutParams generateDefaultLayoutParams() {
166a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell        // Used by custom views if they don't supply layout params. Everything else
167a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell        // added to an ActionBarContextView should have them already.
168a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
169a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell    }
170a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell
171a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell    @Override
17289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
17389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
17489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (widthMode != MeasureSpec.EXACTLY) {
17589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
17689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                    "with android:layout_width=\"match_parent\" (or fill_parent)");
17789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
17889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
17989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
18089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (heightMode != MeasureSpec.AT_MOST) {
18189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
18289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                    "with android:layout_height=\"wrap_content\"");
18389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
18489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
18589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int contentWidth = MeasureSpec.getSize(widthMeasureSpec);
18689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int itemMargin = mItemPadding;
18789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
18889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight();
18989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int height = mContentHeight - getPaddingTop() - getPaddingBottom();
19089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
19189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
19289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCloseButton != null) {
19389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            availableWidth = measureChildView(mCloseButton, availableWidth,
19489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                    childSpecHeight, itemMargin);
19589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
19689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
1970e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout != null && mCustomView == null) {
1980e94b5151d817e600a888448a662208b29b5ef46Adam Powell            availableWidth = measureChildView(mTitleLayout, availableWidth,
19989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                    childSpecHeight, itemMargin);
20089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
20189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
20289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int childCount = getChildCount();
20389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        for (int i = 0; i < childCount; i++) {
20489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            final View child = getChildAt(i);
2050e94b5151d817e600a888448a662208b29b5ef46Adam Powell            if (child == mCloseButton || child == mTitleLayout || child == mCustomView) {
20689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                continue;
20789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            }
20889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
20989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            availableWidth = measureChildView(child, availableWidth, childSpecHeight, itemMargin);
21089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
21189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
21289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCustomView != null) {
213a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            LayoutParams lp = mCustomView.getLayoutParams();
214a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ?
215a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
216a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customWidth = lp.width >= 0 ?
217a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    Math.min(lp.width, availableWidth) : availableWidth;
218a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ?
219a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
220a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            final int customHeight = lp.height >= 0 ?
221a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    Math.min(lp.height, height) : height;
222a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell            mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode),
223a7db03705f53c59e63e63c2e67e2db78f8226dccAdam Powell                    MeasureSpec.makeMeasureSpec(customHeight, customHeightMode));
22489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
22589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
22689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        setMeasuredDimension(contentWidth, mContentHeight);
22789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
22889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
22989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    @Override
23089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    protected void onLayout(boolean changed, int l, int t, int r, int b) {
23189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int x = getPaddingLeft();
23289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int y = getPaddingTop();
23389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int contentHeight = b - t - getPaddingTop() - getPaddingBottom();
23489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        final int itemMargin = mItemPadding;
23589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
23689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCloseButton != null && mCloseButton.getVisibility() != GONE) {
23789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            x += positionChild(mCloseButton, x, y, contentHeight);
23889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
23989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
2400e94b5151d817e600a888448a662208b29b5ef46Adam Powell        if (mTitleLayout != null && mCustomView == null) {
2410e94b5151d817e600a888448a662208b29b5ef46Adam Powell            x += positionChild(mTitleLayout, x, y, contentHeight) + itemMargin;
24289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
24389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
24489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        if (mCustomView != null) {
24589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell            x += positionChild(mCustomView, x, y, contentHeight) + itemMargin;
24689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
24789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
24889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        x = r - l - getPaddingRight();
24989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
2502c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell        if (mMenuView != null) {
2512c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell            x -= positionChildInverse(mMenuView, x + mActionSpacing, y, contentHeight)
2522c9c9fe806e51f29276e66b8ba40cb4b7a80c3f4Adam Powell                    - mActionSpacing;
25389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        }
25489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
25589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
25689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int measureChildView(View child, int availableWidth, int childSpecHeight, int spacing) {
25789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        child.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST),
25889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell                childSpecHeight);
25989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
26089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        availableWidth -= child.getMeasuredWidth();
26189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        availableWidth -= spacing;
26289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
26389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        return availableWidth;
26489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
26589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
26689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int positionChild(View child, int x, int y, int contentHeight) {
26789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childWidth = child.getMeasuredWidth();
26889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childHeight = child.getMeasuredHeight();
26989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childTop = y + (contentHeight - childHeight) / 2;
27089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
27189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        child.layout(x, childTop, x + childWidth, childTop + childHeight);
27289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
27389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        return childWidth;
27489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
27589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
27689e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    private int positionChildInverse(View child, int x, int y, int contentHeight) {
27789e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childWidth = child.getMeasuredWidth();
27889e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childHeight = child.getMeasuredHeight();
27989e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        int childTop = y + (contentHeight - childHeight) / 2;
28089e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
28189e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        child.layout(x - childWidth, childTop, x, childTop + childHeight);
28289e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell
28389e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell        return childWidth;
28489e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell    }
28589e0645b4157961e8c465eb9c819f965fdb453d8Adam Powell}
286