欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android PopupWindow(弹出窗体)

程序员文章站 2022-05-31 15:55:56
...

PopupWindow(弹出窗体)

PupopWindow的方法

setContentView()_设置自定义布局
setwidth()_设置宽度
setHeight()_设置高度
showAsDropDown()_设置出现的位置,你自己自定的控件
showAtLocation()_设置出现的位置,相对父容器

代码

showAsDropDown设置的位置
Android PopupWindow(弹出窗体)
Activity

// An highlighted block
public class MainActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.main_text);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 showPopupWindow();
            }
        });
    }

弹出框代码

// An highlighted block
public void showPopupWindow(){
        PopupWindow popupWindow=new PopupWindow(this);

        View view=View.inflate(MainActivity.this,R.layout.popupview,null);
        popupWindow.setContentView(view);

        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(textView,100,100);
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.alpha=0.5f;
        getWindow().setAttributes(layoutParams);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
                layoutParams.alpha=1.0f;
                getWindow().setAttributes(layoutParams);
            }
        });
    }

showAtLocation()
Android PopupWindow(弹出窗体)

// An highlighted block
public void showPopupWindow(){
        PopupWindow popupWindow=new PopupWindow(this);

        View view=View.inflate(MainActivity.this,R.layout.popupview,null);
        popupWindow.setContentView(view);

        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        View parent=View.inflate(MainActivity.this,R.layout.activity_main,null);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM,0,0);

        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.alpha=0.5f;
        getWindow().setAttributes(layoutParams);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
                layoutParams.alpha=1.0f;
                getWindow().setAttributes(layoutParams);
            }
        });
    }

自定义弹出框
Android PopupWindow(弹出窗体)

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        MenuItem item = menu.findItem(R.id.item3);
        MenuItem item1 = menu.findItem(R.id.item4);
        item.setActionView(R.layout.customview);
        item1.setActionView(R.layout.customview);

        View viewById = item.getActionView().findViewById(R.id.custom_img);
        View viewById1 = item.getActionView().findViewById(R.id.custom_text);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
}