实现将一个View显示在某一位置,而且是浮于当前窗口
首先要有一个要显示的view的布局,可以是任意View,包括ViewGroup
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#d3d3d3"
android:gravity="center_horizontal"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/textView"
android:text="文本内容"
/>
</LinearLayout>
然后主界面布局文件
<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"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!-- 区域 -->
<Button
android:layout_width="0dp"
android:layout_weight=""
android:gravity="center"
android:background="#0000"
android:layout_height="wrap_content"
android:text="区域"
android:drawableRight="@drawable/title_bar_arrow_nor"
android:id="@+id/btn_1"
/>
<!-- 类别 -->
<Button
android:background="#0000"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="类别"
android:drawableRight="@drawable/title_bar_arrow_nor"
android:id="@+id/btn_2"
/> <!-- 价格 -->
<Button
android:background="#0000"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="价格"
android:drawableRight="@drawable/title_bar_arrow_nor"
android:id="@+id/btn_3"
/> <!-- 更多 -->
<Button
android:background="#0000"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:text="更多"
android:drawableRight="@drawable/title_bar_arrow_nor"
android:id="@+id/btn_4"
/> </LinearLayout> </RelativeLayout>
主activity
package com.example.aaa; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private PopupWindow pop; private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4; private TextView text; View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
initPopWindow(); } private void initView() {
// TODO Auto-generated method stub
btn1 = (Button) findViewById(R.id.btn_1);
btn2 = (Button) findViewById(R.id.btn_2);
btn3 = (Button) findViewById(R.id.btn_3);
btn4 = (Button) findViewById(R.id.btn_4);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
} private void initPopWindow() {
// TODO Auto-generated method stub
//根据layout创建弹出界面
view = this.getLayoutInflater().inflate(R.layout.popup_window, null);
pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
text = (TextView) view.findViewById(R.id.textView); pop.setOutsideTouchable(true);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pop.dismiss();
}
}); } @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_1:
text.setText("区域");
if(pop.isShowing())
pop.dismiss();
else
pop.showAsDropDown(v);
break;
case R.id.btn_2:
text.setText("类别");
if(pop.isShowing())
pop.dismiss();
else
pop.showAsDropDown(v);
break;
case R.id.btn_3:
text.setText("价格");
if(pop.isShowing())
pop.dismiss();
else
pop.showAsDropDown(v);
break;
case R.id.btn_4:
text.setText("更多");
if(pop.isShowing())
pop.dismiss();
else
pop.showAsDropDown(v);
break; default:
break;
}
} }
出现位置的几个方法
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
效果图: