1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v7.internal.widget; 18 19import android.annotation.TargetApi; 20import android.content.Context; 21import android.content.res.TypedArray; 22import android.graphics.drawable.Drawable; 23import android.os.Build; 24import android.support.v7.appcompat.R; 25import android.util.AttributeSet; 26import android.view.View; 27import android.widget.PopupWindow; 28 29/** 30 * @hide 31 */ 32public class AppCompatPopupWindow extends PopupWindow { 33 34 private final boolean mOverlapAnchor; 35 36 public AppCompatPopupWindow(Context context, AttributeSet attrs, int defStyleAttr) { 37 super(context, attrs, defStyleAttr); 38 39 TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, 40 R.styleable.PopupWindow, defStyleAttr, 0); 41 mOverlapAnchor = a.getBoolean(R.styleable.PopupWindow_overlapAnchor, false); 42 // We re-set this for tinting purposes 43 setBackgroundDrawable(a.getDrawable(R.styleable.PopupWindow_android_popupBackground)); 44 a.recycle(); 45 } 46 47 @Override 48 public void showAsDropDown(View anchor, int xoff, int yoff) { 49 if (Build.VERSION.SDK_INT < 21 && mOverlapAnchor) { 50 // If we're pre-L, emulate overlapAnchor by modifying the yOff 51 yoff -= anchor.getHeight(); 52 } 53 super.showAsDropDown(anchor, xoff, yoff); 54 } 55 56 @TargetApi(Build.VERSION_CODES.KITKAT) 57 @Override 58 public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) { 59 if (Build.VERSION.SDK_INT < 21 && mOverlapAnchor) { 60 // If we're pre-L, emulate overlapAnchor by modifying the yOff 61 yoff -= anchor.getHeight(); 62 } 63 super.showAsDropDown(anchor, xoff, yoff, gravity); 64 } 65 66 @Override 67 public void update(View anchor, int xoff, int yoff, int width, int height) { 68 if (Build.VERSION.SDK_INT < 21 && mOverlapAnchor) { 69 // If we're pre-L, emulate overlapAnchor by modifying the yOff 70 yoff -= anchor.getHeight(); 71 } 72 super.update(anchor, xoff, yoff, width, height); 73 } 74} 75