这是项目的俯瞰图:
现在首先是main的xml文件布局:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:gravity="center_horizontal"
android:id="@+id/mybutton"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/aboutname"
android:textSize="28sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="京" /> <ImageView
android:paddingTop="5dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/jiantou"/>
</LinearLayout>
</RelativeLayout>
界面如图:
下面是需要悬浮的布局文件。效果如图:
其xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/setname"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TableRow
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_width="match_parent">
<TextView
android:id="@+id/beijing"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="京"
android:padding="8dp"/>
<TextView
android:id="@+id/tianjing"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="津"
android:padding="8dp"/>
<TextView
android:id="@+id/shanghai"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="沪"
android:padding="6dp"/>
<TextView
android:id="@+id/chongqin"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="渝"
android:padding="6dp"/>
<TextView
android:id="@+id/hebei"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="冀"
android:padding="6dp"/>
<TextView
android:id="@+id/henan"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="豫"
android:padding="6dp"/>
<TextView
android:id="@+id/yunnan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="云"
android:textSize="25dp" />
<TextView
android:id="@+id/liaonin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="辽"
android:textSize="25dp" />
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent">
<TextView
android:id="@+id/hlj"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="黑"
android:padding="6dp"/>
<TextView
android:id="@+id/hunan"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="湘"
android:padding="6dp"/>
<TextView
android:id="@+id/anhui"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="皖"
android:padding="6dp"/>
<TextView
android:id="@+id/shandong"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="鲁"
android:padding="6dp"/>
<TextView
android:id="@+id/*"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="新"
android:padding="6dp"/>
<TextView
android:id="@+id/jiangsu"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="苏"
android:padding="6dp"/>
<TextView
android:id="@+id/zhejiang"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="浙"
android:padding="6dp"/>
<TextView
android:id="@+id/jiangxi"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="赣"
android:padding="6dp"/>
</TableRow> <TableRow
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent">
<TextView
android:id="@+id/hubei"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="鄂"
android:padding="6dp"/>
<TextView
android:id="@+id/guangxi"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="桂"
android:padding="6dp"/>
<TextView
android:id="@+id/gansu"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="甘"
android:padding="6dp"/>
<TextView
android:id="@+id/shanxi"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="晋"
android:padding="6dp"/>
<TextView
android:id="@+id/neimen"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="蒙"
android:padding="6dp"/>
<TextView
android:id="@+id/xian"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="陕"
android:padding="6dp"/>
<TextView
android:id="@+id/jiling"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="吉"
android:padding="6dp"/>
<TextView
android:id="@+id/fujian"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=" 闽"
android:padding="6dp"/>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_width="match_parent">
<TextView
android:id="@+id/guizhou"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=" 贵"
android:padding="6dp"/>
<TextView
android:id="@+id/guangdong"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="粤"
android:padding="6dp"/>
<TextView
android:id="@+id/qinghai"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="青"
android:padding="6dp"/>
<TextView
android:id="@+id/xizang"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="藏"
android:padding="6dp"/>
<TextView
android:id="@+id/sichuan"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="川"
android:padding="6dp"/>
<TextView
android:id="@+id/ninxia"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="宁"
android:padding="6dp"/>
<TextView
android:id="@+id/hainan"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=" 琼"
android:padding="6dp"/>
<TextView
android:id="@+id/*"
android:textSize="25dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="台"
android:padding="6dp"/>
</TableRow>
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:layout_marginTop="15dp"
android:background="@drawable/bg_btn"
android:text="取消"
android:textColor="#FFFFFF"
android:textSize="22sp" />
</LinearLayout>
</LinearLayout>
这是主方法里面的代码:
package com.example.mypopwindowdemo;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends Activity {
LinearLayout mybutton;
PopupWindow popWin;
Button cancel;
View popView;
View setname;
TextView aboutname;
String info;
TextView beijing,tianjing,shanghai,chongqin,hebei, henan,yunnan,liaonin,hlj,hunan, anhui,shandong,*,zhejiang,jiangxi, hubei,guangxi,gansu,shanxi,neimen, xian,jiling,fujian,guizhou,guangdong, qinhai,xizang,sichuan,ninxia,hainan,*,jiangsu;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
find();
// mybutton是textview和小三角的组合的Linearlayout,给其设置监听
mybutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 取得LayoutInflater对象主要作用是 用来获取布局文件
LayoutInflater lay = LayoutInflater.from(MainActivity.this);
// 读取布局管理器,换句话说就是讲布局文件读到一个view当中
MainActivity.this.popView = lay.inflate(R.layout.myxiao, null);
// 实例化popwindow
popWin = new PopupWindow(popView,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,true);
//Gravity.BOTTOM这里是这个悬浮层是从底部显示
popWin.showAtLocation(MainActivity.this.mybutton, Gravity.BOTTOM, 0, 0);
findpopwin();
listener();
setname = popView.findViewById(R.id.setname);
cancel = (Button) popView.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 关闭弹出窗口
popWin.dismiss(); } }); }
// 用来获取popwin里面的每个textview的id
private void findpopwin() {
// TODO Auto-generated method stub
beijing = (TextView) popView.findViewById(R.id.beijing);
tianjing = (TextView) popView.findViewById(R.id.tianjing);
shanghai = (TextView)popView.findViewById(R.id.shanghai);
chongqin = (TextView)popView.findViewById(R.id.chongqin);
hebei = (TextView)popView.findViewById(R.id.hebei);
henan = (TextView)popView.findViewById(R.id.henan);
yunnan = (TextView)popView.findViewById(R.id.yunnan);
liaonin = (TextView)popView.findViewById(R.id.liaonin);
hlj = (TextView)popView.findViewById(R.id.hlj);
hunan = (TextView)popView.findViewById(R.id.hunan);
anhui = (TextView)popView.findViewById(R.id.anhui);
shandong = (TextView)popView.findViewById(R.id.shandong);
* = (TextView)popView.findViewById(R.id.*);
jiangsu = (TextView)popView.findViewById(R.id.jiangsu);
zhejiang = (TextView)popView.findViewById(R.id.zhejiang);
jiangxi = (TextView)popView.findViewById(R.id.jiangxi);
hubei = (TextView)popView.findViewById(R.id.hubei);
guangxi = (TextView)popView.findViewById(R.id.guangxi);
gansu = (TextView)popView.findViewById(R.id.gansu);
shanxi = (TextView)popView.findViewById(R.id.shanxi);
neimen = (TextView)popView.findViewById(R.id.neimen);
xian = (TextView)popView.findViewById(R.id.xian);
jiling = (TextView)popView.findViewById(R.id.jiling);
fujian = (TextView)popView.findViewById(R.id.fujian);
guizhou = (TextView)popView.findViewById(R.id.guizhou);
guangdong = (TextView)popView.findViewById(R.id.guangdong);
qinhai = (TextView)popView.findViewById(R.id.qinghai);
xizang = (TextView)popView.findViewById(R.id.xizang);
sichuan = (TextView)popView.findViewById(R.id.sichuan);
ninxia = (TextView)popView.findViewById(R.id.ninxia);
hainan = (TextView)popView.findViewById(R.id.hainan);
* = (TextView)popView.findViewById(R.id.*);
} });
}
// 给popwin里面的每个值设置产生监听的操作
private void listener() {
beijing.setOnClickListener(new OnListenerImple());
tianjing.setOnClickListener(new OnListenerImple());
shanghai.setOnClickListener(new OnListenerImple());
chongqin.setOnClickListener(new OnListenerImple());
hebei.setOnClickListener(new OnListenerImple());
henan.setOnClickListener(new OnListenerImple());
yunnan.setOnClickListener(new OnListenerImple());
liaonin.setOnClickListener(new OnListenerImple());
hlj.setOnClickListener(new OnListenerImple());
hunan.setOnClickListener(new OnListenerImple());
anhui.setOnClickListener(new OnListenerImple());
shandong.setOnClickListener(new OnListenerImple());
*.setOnClickListener(new OnListenerImple());
jiangsu.setOnClickListener(new OnListenerImple());
zhejiang.setOnClickListener(new OnListenerImple());
jiangxi.setOnClickListener(new OnListenerImple());
hubei.setOnClickListener(new OnListenerImple());
guangxi.setOnClickListener(new OnListenerImple());
gansu.setOnClickListener(new OnListenerImple());
shanxi.setOnClickListener(new OnListenerImple());
neimen.setOnClickListener(new OnListenerImple());
xian.setOnClickListener(new OnListenerImple());
jiling.setOnClickListener(new OnListenerImple());
fujian.setOnClickListener(new OnListenerImple());
guizhou.setOnClickListener(new OnListenerImple());
guangdong.setOnClickListener(new OnListenerImple());
qinhai.setOnClickListener(new OnListenerImple());
xizang.setOnClickListener(new OnListenerImple());
sichuan.setOnClickListener(new OnListenerImple());
ninxia.setOnClickListener(new OnListenerImple());
hainan.setOnClickListener(new OnListenerImple());
*.setOnClickListener(new OnListenerImple());
}
private void find() {
// TODO Auto-generated method stub
aboutname = (TextView)findViewById(R.id.aboutname);
mybutton = (LinearLayout)findViewById(R.id.mybutton);
cancel = (Button)findViewById(R.id.cancel); }
// 单击popwin里面的每一项的时候产生的操作就是讲本身的text赋值给前面activity_main里面的textview
private class OnListenerImple implements OnClickListener{
@Override
public void onClick(View vv) {
// TODO Auto-generated method stub
TextView tv = (TextView)vv;
switch (vv.getId()) {
case R.id.beijing:
info = beijing.getText().toString();
aboutname.setText(info);
popWin.dismiss(); break;
case R.id.shanghai:
info = shanghai.getText().toString();
aboutname.setText(info);
popWin.dismiss(); break;
case R.id.tianjing:
info = tianjing.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break; case R.id.chongqin:
info = chongqin.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.hebei:
info = hebei.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.henan:
info = henan.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.yunnan:
info = yunnan.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break; case R.id.liaonin:
info = liaonin.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break; case R.id.hlj:
info = hlj.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break; case R.id.hunan:
info = hunan.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.anhui:
info = anhui.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.shandong:
info = shandong.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.*:
info = *.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.jiangsu:
info = jiangsu.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.zhejiang:
info = zhejiang.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.jiangxi:
info = jiangxi.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.hubei:
info = hubei.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.guangxi:
info = guangxi.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.gansu:
info = gansu.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.shanxi:
info = shanxi.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.neimen:
info = neimen.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break; case R.id.xian:
info = xian.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.jiling:
info = jiling.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.fujian:
info = fujian.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.guizhou:
info = guizhou.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.guangdong:
info = guangdong.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.qinghai:
info = qinhai.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.xizang:
info = xizang.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.sichuan:
info = sichuan.getText().toString();
aboutname.setText(info);
popWin.dismiss(); break;
case R.id.ninxia:
info = ninxia.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break; case R.id.hainan:
info = hainan.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break;
case R.id.*:
info = *.getText().toString();
aboutname.setText(info);
popWin.dismiss();
break; } } }
}
这是实现效果图:
这里是源代码: