利用PopupWindow定制信息浮窗
点击地图上的店铺图标弹出浮窗显示店铺部分信息
具体效果
布局文件
<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="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<TextView
android:id="@+id/float_shop_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="27dp"
android:layout_marginTop="8dp"
android:textColor="@color/font_black_21"
android:textSize="16sp"
tools:text="店铺名称:时光音乐饮吧" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="9.5dp"
android:background="@color/view_gray_2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="11.5dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="25.5dp"
android:src="@drawable/home_map_icon" />
<TextView
android:id="@+id/float_shop_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dp"
android:background="@color/white"
android:text="河南省开封市市辖区金耀路王八副书记放"
android:textColor="@color/font_black_11"
android:textSize="@dimen/font_size_14"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="13dp"
android:background="@color/view_gray_2" />
<LinearLayout
android:layout_marginTop="11dp"
android:id="@+id/float_shop_linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:showDividers="middle">
<LinearLayout
android:id="@+id/re2"
android:layout_weight="1"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="距离当前位置"
android:textColor="@color/font_black_21"
android:textSize="13sp"
android:layout_marginLeft="50.5dp"/>
<TextView
android:id="@+id/float_shop_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_gravity="center"
android:text="50m"
android:textColor="@color/font_black_21"
android:textSize="22sp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/view_gray_1" />
<LinearLayout
android:id="@+id/re3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="征信积分"
android:layout_marginLeft="63.5dp"
android:textColor="@color/font_black_21"
android:textSize="13sp" />
<TextView
android:id="@+id/float_shop_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:text="0分"
android:textColor="@color/font_black_21"
android:textSize="22sp" />
</LinearLayout>
</LinearLayout>
- 代码文件
private View mPopWindowView; //PopWindow对应的布局View
/**
* 创建信息浮窗
*
* @param shopItem
*/
private void createPopupWindow(MapShopItem shopItem) {
//...省略部分代码
//获取视图
mPopWindowView = (View) LayoutInflater.from(getActivity()).inflate(R.layout.floating_infowindow,
null);
//获取视图上的控件对象
TextView shopLocationTextView = (TextView) mPopWindowView.findViewById(R.id.float_shop_location);
//获取征信积分
int shopCredit = shopItem.getAnnual_credits();
//获得绑定的数据并为shopNameTextView赋值
TextView shopNameTextView = (TextView) mPopWindowView.findViewById(R.id.float_shop_name);
if(shopName.length()<=12)
shopNameTextView.setText(getResources().getString(R.string.shop_name_tip) + ":" + shopName);
else {
String shortShopName = shopName.substring(0,12) + "...";
shopNameTextView.setText(getResources().getString(R.string.shop_name_tip) + ":" + shortShopName);
}
//...省略部分代码(一些View赋值的语句)
//获取显示地图界面的toolBar
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.shop_toolbar);
//创建PopupWindow
PopupWindow popupWindow = new PopupWindow(mPopWindowView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, true);
//点击其他地方关闭
popupWindow.setOutsideTouchable(true);
//由于Android 版本差异性,在不需要背景的情况下设置背景为透明
popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.map_page_background)));
//设置显示位置
//popupWindow.showAtLocation(mMapView, Gravity.TOP, 0, toolbar.getHeight() + 71);
popupWindow.showAsDropDown(toolbar);//显示在toolbar下方
}
PopupWindow介绍
android.widget.popupWindow,这个类可用于显示任意视图的弹出窗口。弹出窗口是一个浮动的容器,可以根据需要选择显示的位置。
- 构造方法(部分)
//创建一个新的空的,不可聚焦的维(0,0)弹出窗口。
PopupWindow(Context context);
//创建一个新的弹出窗口
PopupWindow(View contentView,int width,int height,boolean focusable);
要生成一个PopupWindow这三个最基本的参数必不可少:View contentView,int width,int height; 其中height和width的值可以是:match_parent,wrap_content,或者具体的数值。
- 显示位置
//显示在某个指定控件的下方
showAsDropDown(View anchor):
//显示在某个指定控件的下方,指定偏移距离
showAsDropDown(View anchor, int xoff, int yoff);
//指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);
//相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor):
//相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
showAsDropDown(View anchor, int xoff, int yoff):
//相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
showAtLocation(View parent, int gravity, int x, int y):
- 关闭PopupWindow
public void dismiss()