使用PopupWindow實現ActionBar下拉選單效果
阿新 • • 發佈:2019-02-20
使用PopupWindow模仿ActionBar下拉選單效果
效果
設定佈局
程式碼
public class PopupOverFlow extends PopupWindow implements View.OnClickListener, PopupWindow.OnDismissListener {
public PopupOverFlow(Context context) {
// 設定SelectPicPopupWindow的View
View contentView = View.inflate(context, R.layout.common_reading_overflow, null );
setContentView(contentView);
// 設定SelectPicPopupWindow彈出窗體的寬
contentView.measure(0,0);
setWidth(contentView.getMeasuredWidth());
// 設定SelectPicPopupWindow彈出窗體的高
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 設定SelectPicPopupWindow彈出窗體可點選
setFocusable(true );
setOutsideTouchable(true);
// 點back鍵和其他地方使其消失,設定了這個才能觸發OnDismisslistener ,設定其他控制元件變化等操作
BitmapDrawable dw = new BitmapDrawable();
setBackgroundDrawable(dw);
// 設定SelectPicPopupWindow彈出窗體動畫效果
setAnimationStyle(R.style.AnimTools);
contentView.findViewById(R.id.overflow_postil).setOnClickListener(this );
contentView.findViewById(R.id.overflow_label).setOnClickListener(this);
contentView.findViewById(R.id.overflow_mandatory).setOnClickListener(this);
contentView.findViewById(R.id.overflow_share).setOnClickListener(this);
setOnDismissListener(this);
}
/**
* 顯示popupWindow
*
* @param parent anchor
*
* @return true: show , false: hide
*/
public boolean showOrHideOverflow(View parent) {
boolean showing = isShowing();
if (!showing) {
// 以下拉方式顯示popupwindow
this.showAsDropDown(parent, 0, 18);
} else {
this.dismiss();
}
return !showing;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.overflow_postil:
if (l != null)
l.onAddStdPostil(v);
break;
case R.id.overflow_label:
if (l != null)
l.onLabelClick(v);
break;
case R.id.overflow_mandatory:
if (l != null)
l.onMandatoryClick(v);
break;
case R.id.overflow_share:
if (l != null)
l.onShareClick(v);
break;
}
this.dismiss();
}
private MenuListener l;
public void setOnMenuClickListener(MenuListener l) {
this.l = l;
}
@Override
public void onDismiss() {
if (l != null)
l.onDismiss();
}
public interface MenuListener {
void onDismiss();
void onAddStdPostil(View v);
void onLabelClick(View v);
void onMandatoryClick(View v);
void onShareClick(View v);
}
}
動畫檔案
- style
<style name="AnimTools" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/push_in</item>
<item name="android:windowExitAnimation">@anim/push_out</item>
</style>
- push_in
<?xml version="1.0" encoding="utf-8"?>
<!-- 左上角擴大-->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0.001"
android:toXScale="1.0"
android:fromYScale="0.001"
android:toYScale="1.0"
android:pivotX="90%"
android:pivotY="0"
android:duration="100" />
- push_out
<?xml version="1.0" encoding="utf-8"?>
<!-- 左上角縮小 -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="0.001"
android:fromYScale="1.0"
android:toYScale="0.001"
android:pivotX="90%"
android:pivotY="0"
android:duration="200" />
佈局檔案
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="@dimen/dp_5">
<LinearLayout
android:layout_width="@dimen/dp_120"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<TextView
android:id="@+id/overflow_postil"
style="@style/std_overflow_item"
android:drawableLeft="@mipmap/icon_annotation"
android:text="@string/overflow_add_postil" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0.2"
android:background="#000" />
<TextView
android:id="@+id/overflow_label"
style="@style/std_overflow_item"
android:drawableLeft="@mipmap/icon_tips"
android:text="@string/overflow_add_label" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0.2"
android:background="#000" />
<TextView
android:id="@+id/overflow_mandatory"
style="@style/std_overflow_item"
android:drawableLeft="@mipmap/icon_warning"
android:text="@string/overflow_mandatory" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0.2"
android:background="#000" />
<TextView
android:id="@+id/overflow_share"
style="@style/std_overflow_item"
android:drawableLeft="@mipmap/icon_share"
android:text="@string/overflow_share" />
</LinearLayout>
</FrameLayout>
設定窗體透明效果
//彈出視窗時結合改變窗體透明度
@OnClick(R.id.title_btn_overflow)
void overflowClick(View v) {
if (popupOverFlow.showOrHideOverflow(v)) {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.alpha = 0.7f;
getWindow().setAttributes(params);
}
}
//關閉時回覆視窗透明度
@Override
public void onDismiss() {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.alpha = 1f;
getWindow().setAttributes(params);
}