弹出后在上面后window需要和RadioGroup紧贴,不是弹到屏幕顶部
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
tools:context="com.de.activity.MainActivity">
<RadioGroup
android:id="@+id/rg_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:clipChildren="true"
android:background="#FFFFFF"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_home"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/home_button_selector"
android:gravity="center"
android:paddingTop="5dp"
android:text="首页"
android:textColor="@drawable/main_radiobutton_text_color_selector" />
<RadioButton
android:id="@+id/rb_channel"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/channel_button_selector"
android:gravity="center"
android:paddingTop="5dp"
android:text="频道"
android:textColor="@drawable/main_radiobutton_text_color_selector" />
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:layout_weight="1" />
<RadioButton
android:id="@+id/rb_discovery"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/discovery_button_selector"
android:gravity="center"
android:paddingTop="5dp"
android:text="发现"
android:textColor="@drawable/main_radiobutton_text_color_selector" />
<RadioButton
android:id="@+id/rb_mine"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/mine_button_selector"
android:gravity="center"
android:paddingTop="5dp"
android:text="我的"
android:textColor="@drawable/main_radiobutton_text_color_selector" />
</RadioGroup>
<View
android:layout_above="@id/rg_tabs"
style="@style/HorizontalLine"/>
<FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/rg_tabs">
</FrameLayout>
<!--中间凸起按钮-->
<ImageView
android:id="@+id/iv_switch_game_normal"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@mipmap/king_honor" />
</RelativeLayout>
类局部代码:
private void showTypeWindow() {
View typeWindow = LayoutInflater.from(MainActivity.this).inflate(R.layout.window_type, null);
mPopupWindow = new PopupWindow(typeWindow, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); //点击空白处popupwindow消失
mPopupWindow.setFocusable(true);
//TODO:弹出window显示在底部button上面
int[] location = new int[2];
mSwitchGameNormal.getLocationOnScreen(location);
mPopupWindow.showAtLocation(mSwitchGameNormal, Gravity.NO_GRAVITY, location[0], location[1]-mPopupWindow.getHeight());
// mPopupWindow.showAtLocation(mSwitchGameNormal, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //popupwindow弹出的位置
WindowManager.LayoutParams lpType = getWindow().getAttributes(); // 设置背景颜色变暗
lpType.alpha = 0.7f;
getWindow().setAttributes(lpType);
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});
TextView heroUnion = typeWindow.findViewById(R.id.rl_hero_union);
TextView kingHonor = typeWindow.findViewById(R.id.rl_king_honor);
TextView wildrnessAction = typeWindow.findViewById(R.id.rl_wilderness_action);
heroUnion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "英雄联盟", Toast.LENGTH_SHORT).show();
mSwitchGameNormal.setImageResource(R.mipmap.hero_union);
mPopupWindow.dismiss();
}
});
kingHonor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "王者荣耀", Toast.LENGTH_SHORT).show();
mSwitchGameNormal.setImageResource(R.mipmap.king_honor);
mPopupWindow.dismiss();
}
});
wildrnessAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "荒野行动", Toast.LENGTH_SHORT).show();
mSwitchGameNormal.setImageResource(R.mipmap.wilderness_action);
mPopupWindow.dismiss();
}
});
}
需要的效果图:
我之前也做过这种,只能给你提供个思路
实现思路:先设为透明弹出popupWindow,测量其宽高,算出对应偏移量,然后精确位置弹出popupWindow
如果你已经知道要弹出的popupWindow大小了,那就直接计算出位置,然后弹出