popupwindow的简单使用。
主界面布局:main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ningshengcai.popupwindow.MainActivity">
<Button
android:id="@+id/popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/popupwindow"
android:layout_centerHorizontal="true" />
</RelativeLayout>
自定义popupwindiw 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#0066cc"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:text="toast"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"/>
</LinearLayout>
主代码:MainActivity.java
package com.example.ningshengcai.popupwindow;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button popupwindow;
private PopupWindow mpopupwindow;
boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initUI();
}
private void initUI() {
popupwindow = (Button)findViewById(R.id.popup);
popupwindow.setOnClickListener(new popupwindowListener());
}
public class popupwindowListener implements View.OnClickListener{
@Override
public void onClick(View v) {
if (flag){
showpopupwindow();//打开popupwindow
mpopupwindow.showAsDropDown(v);//设置弹出的位置,控件下方
// mpopupwindow.showAsDropDown(View anchor, int xoff, int yoff)//相对某个控件的位置,有偏移
flag = false;
}else if (!flag){
mpopupwindow.dismiss();//关闭popupwindow
//更新窗口的位置
// mpopupwindow.update(300, 300, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
flag=true;
}
}
}
private void showpopupwindow(){
View contentView = LayoutInflater.from(this).inflate(R.layout.popup, null);//设置contentview
mpopupwindow = new PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);//设置线性默认宽高
// 设置弹出窗体需要软键盘,INPUT_METHOD_FROM_FOCUSABLE, INPUT_METHOD_NEEDED, INPUT_METHOD_NOT_NEEDED.
// mpopupwindow.setInputMethodMode(PopupWindow.INPUT_METHOD_FROM_FOCUSABLE);
//mpopupwindow.setWidth(100);//设置窗口宽度
// mpopupwindow.setHeight(200);//设置窗口高度
Button btn = (Button)contentView.findViewById(R.id.btn);
final TextView text = (TextView) contentView.findViewById(R.id.text);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText("toast");
Toast.makeText(getApplication(),"nsc",Toast.LENGTH_SHORT).show();
}
});
}
}