-
1.Activity简单布局
效果图省略,下面是activity内的布局文件 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.弹出窗布局
创建布局文件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.动画布局
在res的anim文件夹下创建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);
// 设置布局文件
View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_friend_more, null);
mPopupWindow.setContentView(contentView);
// 为了避免部分机型不显示,我们需要重新设置一下宽高
mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// 设置pop透明效果
mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000));
// 设置pop出入动画
mPopupWindow.setAnimationStyle(R.style.pop_add);
// 设置pop获取焦点,如果为false点击返回按钮会退出当前Activity,如果pop中有Editor的话,focusable必须要为true
mPopupWindow.setFocusable(true);
// 设置pop可点击,为false点击事件无效,默认为true
mPopupWindow.setTouchable(true);
// 设置点击pop外侧消失,默认为false;在focusable为true时点击外侧始终消失
mPopupWindow.setOutsideTouchable(true);
// 相对于 + 号正下面,同时可以设置偏移量
mPopupWindow.showAsDropDown(imgBtnAddF, -100, 0);
backgroundAlpha(0.7f);
// 设置pop关闭监听,用于改变背景透明度
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();
}
});
}
/**
* 此方法用于改变背景的透明度,从而达到“变暗”的效果
*/
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.
// 此方法用来设置浮动层,防止部分手机变暗无效
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}