-
1.Activity簡單布局
效果圖省略,下面是activity內(nèi)的布局文件 R.layout.main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="@dimen/dp_24">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_60"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageButton
android:id="@+id/img_btn_back"
android:layout_width="@dimen/dp_45"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dp_10"
android:background="@color/transparent"
android:src="@drawable/ic_back" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_20"
android:layout_weight="1"
android:text="@string/fd"
android:textColor="@color/black_font"
android:textSize="@dimen/sp_18" />
<ImageButton
android:id="@+id/img_btn_add_f"
android:layout_width="45dp"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/dp_10"
android:background="@color/transparent"
android:src="@drawable/ic_friend_more" />
</LinearLayout>
</LinearLayout>
-
2.彈出窗布局
創(chuàng)建布局文件R.layout.dialog_friend_more.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/friend_pop_bg"
android:gravity="center"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/new_friend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_add_friend"
android:drawablePadding="@dimen/dp_15"
android:gravity="center"
android:padding="10dp"
android:text="新朋友"
android:textColor="@color/colorGray"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ECECEC"/>
<TextView
android:id="@+id/back_friend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_black"
android:drawablePadding="@dimen/dp_15"
android:gravity="center"
android:padding="10dp"
android:text="黑名單"
android:textColor="@color/colorGray"/>
</LinearLayout>
</LinearLayout>
-
3.動(dòng)畫布局
在res的anim文件夾下創(chuàng)建pop_add_show.xml和pop_add_hide.xml
<style name="pop_add">
<item name="android:windowEnterAnimation">@anim/pop_add_show</item>
<item name="android:windowExitAnimation">@anim/pop_add_hide</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="@integer/config_pop_duration"
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
<scale
android:duration="@integer/config_pop_duration"
android:fromXScale="0"
android:fromYScale="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:pivotX="85%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="@integer/config_pop_duration"
android:fromAlpha="1.0"
android:toAlpha="0.0"/>
<scale
android:duration="@integer/config_pop_duration"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="85%"
android:pivotY="0%"
android:toXScale="0"
android:toYScale="0"/>
</set>
-
4.使用
private void showPop() {
PopupWindow mPopupWindow = new PopupWindow(this);
// 設(shè)置布局文件
View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_friend_more, null);
mPopupWindow.setContentView(contentView);
// 為了避免部分機(jī)型不顯示嫂侍,我們需要重新設(shè)置一下寬高
mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 設(shè)置pop透明效果
mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000));
// 設(shè)置pop出入動(dòng)畫
mPopupWindow.setAnimationStyle(R.style.pop_add);
// 設(shè)置pop獲取焦點(diǎn)驱证,如果為false點(diǎn)擊返回按鈕會(huì)退出當(dāng)前Activity雕拼,如果pop中有Editor的話,focusable必須要為true
mPopupWindow.setFocusable(true);
// 設(shè)置pop可點(diǎn)擊歼捏,為false點(diǎn)擊事件無效,默認(rèn)為true
mPopupWindow.setTouchable(true);
// 設(shè)置點(diǎn)擊pop外側(cè)消失笨篷,默認(rèn)為false瞳秽;在focusable為true時(shí)點(diǎn)擊外側(cè)始終消失
mPopupWindow.setOutsideTouchable(true);
// 相對(duì)于 + 號(hào)正下面,同時(shí)可以設(shè)置偏移量
mPopupWindow.showAsDropDown(imgBtnAddF, -100, 0);
backgroundAlpha(0.7f);
// 設(shè)置pop關(guān)閉監(jiān)聽率翅,用于改變背景透明度
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha(1.0f);
}
});
mPopupWindow.getContentView().findViewById(R.id.new_friend).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, FriendRequireListActivity.class));
mPopupWindow.dismiss();
}
});
mPopupWindow.getContentView().findViewById(R.id.back_friend).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(mContext, FriendBlackListActivity.class));
mPopupWindow.dismiss();
}
});
}
/**
* 此方法用于改變背景的透明度练俐,從而達(dá)到“變暗”的效果
*/
private void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
// 0.0-1.0
lp.alpha = bgAlpha;
getWindow().setAttributes(lp);
// everything behind this window will be dimmed.
// 此方法用來設(shè)置浮動(dòng)層,防止部分手機(jī)變暗無效
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}