对于自定义窗口,Android提供了PopupWindow,简单实用。
下面我们来看看:
PopupWindow的构造函数
public PopupWindow(View contentView, int width, int height, boolean focusable)
其中contentView为要显示的view,width和height为宽和高,值为像素值,可以是MATCHT_PARENT和WRAP_CONTENT
来设置,如果focusable为false,在一个Activity弹出一个PopupWindow,按返回键,由于PopupWindow没有焦点,会直接退出Activity。如果focusable为true,PopupWindow弹出后,所有的触屏和物理按键都有PopupWindows处理。
如果PopupWindow中有Editor的话,focusable要为true。
下面来看一个简单的demo
主界面:就简单放一个button,点击弹出我们需要的PopupWindow
[html] view plaincopyprint?
-
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
android:id="@+id/container"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent" >
-
<Button
-
android:id="@+id/btnOpen"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_centerInParent="true"
-
android:text="@string/app_name"/>
-
</RelativeLayout>
popupwindow 界面:放了2个button 和一个文本框,用来输入值
[html] view plaincopyprint?
-
<?xml version="1.0"encoding="utf-8"?>
-
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:background="#b5555555">
-
<LinearLayout
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:layout_alignParentBottom="true"
-
android:background="#eee"
-
android:orientation="vertical" >
-
<EditText
-
android:id="@+id/leaveword"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_marginBottom="20dp"
-
android:layout_marginLeft="20dp"
-
android:layout_marginRight="20dp"
-
android:layout_marginTop="20dp"
-
android:gravity="top"
-
android:hint="说点什么吧~"
-
android:inputType="textMultiLine"
-
android:lineSpacingExtra="6.0dp"
-
android:maxHeight="150dp"
-
android:minHeight="100dp"
-
android:paddingLeft="10.0dp"
-
android:paddingRight="10.0dp"
-
android:paddingTop="10.0dp" />
-
<LinearLayout
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_marginBottom="20dp"
-
android:gravity="center"
-
android:orientation="horizontal">
-
<Button
-
android:id="@+id/confirmButton"
-
android:layout_width="80.0dip"
-
android:layout_height="wrap_content"
-
android:gravity="center"
-
android:text="发表"
-
android:textColor="#fff"
-
android:textSize="16.0sp" />
-
<Button
-
android:id="@+id/cancleButton"
-
android:layout_width="80.0dip"
-
android:layout_height="wrap_content"
-
android:layout_marginLeft="30dp"
-
android:gravity="center"
-
android:text="取消"
-
android:textColor="#565656"
-
android:textSize="16.0sp" />
-
</LinearLayout>
-
</LinearLayout>
-
</RelativeLayout>
效果:
这样我们的界面就算完成了,下来我们来看一个Activity。写个了OpenView 来弹出view
[java] view plaincopyprint?
-
public void OpenView() {
-
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
-
popupWindowView = inflater.inflate(R.layout.popupwindow, null);
-
popupWindow = newPopupWindow(popupWindowView,
-
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,true);
-
// 设置PopupWindow的弹出和消失效果
-
popupWindow.setAnimationStyle(R.style.popupAnimation);
-
btnsure = (Button) popupWindowView.findViewById(R.id.confirmButton);
-
btnsure.setOnClickListener(newButtonOnClickListener());
-
cancleButton = (Button) popupWindowView.findViewById(R.id.cancleButton);
-
cancleButton.setOnClickListener(new ButtonOnClickListener());
-
leaveword = (EditText) popupWindowView.findViewById(R.id.leaveword);
-
popupWindow.showAtLocation(btnsure, Gravity.CENTER, 0, 0);
-
}
我们看到弹出来的有点动画效果,是因为我们在弹出时,加上了
[java] view plaincopyprint?
-
popupWindow.setAnimationStyle(R.style.popupAnimation);
需要在styles.xml下加上
[html] view plaincopyprint?
-
<style name="popupAnimation"parent="android:Animation">
-
<itemname="android:windowEnterAnimation">@anim/in</item>
-
<itemname="android:windowExitAnimation">@anim/out</item>
-
lt;/style>
在 在工程res下新建anim文件夹,在anim文件夹先新建两个xml文件
in.xml
[html] view plaincopyprint?
-
<?xml version="1.0"encoding="utf-8"?>
-
<setxmlns:android="http://schemas.android.com/apk/res/android" >
-
-
<translate
-
android:duration="1500"
-
android:fromYDelta="5000"
-
android:toYDelta="0" />
-
-
</set>
out.xml
[html] view plaincopyprint?
-
<?xml version="1.0"encoding="utf-8"?>
-
<setxmlns:android="http://schemas.android.com/apk/res/android">
-
<translate
-
android:fromYDelta="0"
-
android:toYDelta="5000"
-
android:duration="1500"
-
/>
-
</set>
如果想要关闭弹出框
调用popupWindow.dismiss();就好
这样一个简单的PopupWindow例子就算完成了
[java] view plaincopyprint?
-
public class MainActivity extendsActivity {
-
private View popupWindowView;
-
private PopupWindow popupWindow;
-
private Button btnsure, cancleButton, btnOpen;
-
private EditText leaveword;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
btnOpen = (Button) findViewById(R.id.btnOpen);
-
btnOpen.setOnClickListener(newButtonOnClickListener());
-
}
-
-
public void OpenView() {
-
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
-
popupWindowView = inflater.inflate(R.layout.popupwindow, null);
-
popupWindow = newPopupWindow(popupWindowView,
-
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,true);
-
// 设置PopupWindow的弹出和消失效果
-
popupWindow.setAnimationStyle(R.style.popupAnimation);
-
btnsure = (Button) popupWindowView.findViewById(R.id.confirmButton);
-
btnsure.setOnClickListener(newButtonOnClickListener());
-
cancleButton = (Button) popupWindowView.findViewById(R.id.cancleButton);
-
cancleButton.setOnClickListener(new ButtonOnClickListener());
-
leaveword = (EditText) popupWindowView.findViewById(R.id.leaveword);
-
popupWindow.showAtLocation(btnsure, Gravity.CENTER, 0, 0);
-
}
-
-
private classButtonOnClickListenerimplements OnClickListener {
-
@Override
-
public void onClick(View vid) {
-
-
switch (vid.getId()) {
-
-
case R.id.btnOpen:
-
OpenView();
-
break;
-
case R.id.confirmButton:
-
-
Toast.makeText(MainActivity.this, leaveword.getText().toString(), Toast.LENGTH_SHORT)
-
.show();
-
-
break;
-
case R.id.cancleButton:
-
popupWindow.dismiss();
-
break;
-
-
default:
-
break;
-
}
-
}
-
}
-
-
}
https://mp.weixin.qq.com/s/AzCA5ehDapYA7xsRoc5fjQ