当前位置: 首页 > 工具软件 > Tile Menu > 使用案例 >

Menu菜单和PopupWindow窗口

楚瑞
2023-12-01
public class MainActivity extends AppCompatActivity {
    private ImageView add;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add = findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PopupWindow popupWindow = new PopupWindow(MainActivity.this);
                View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_one, null);
                TextView textView = inflate.findViewById(R.id.layout_one);
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, "测试", Toast.LENGTH_SHORT).show();
                    }
                });


                popupWindow.setContentView(inflate);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                popupWindow.setOutsideTouchable(true);

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


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

                View inflate1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);
                popupWindow.showAtLocation(inflate1, Gravity.CENTER,0,0);

            }
        });

    }
    private void method2() {
        PopupWindow popupWindow = new PopupWindow(MainActivity.this);
        View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_one, null);
        popupWindow.setContentView(inflate);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

        popupWindow.setOutsideTouchable(true);

        View inflate1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);
        popupWindow.showAtLocation(inflate1, Gravity.BOTTOM,0,0);
    }

    private void method1() {
        PopupWindow popupWindow = new PopupWindow(MainActivity.this);
        View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_one, null);
        popupWindow.setContentView(inflate);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(600);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(add,0,0);
    }
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:background="#C0CCCC"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        >
        <TextView
            android:gravity="center_vertical"
            android:textColor="#585656"
            android:textSize="20sp"
            android:layout_marginLeft="30dp"
            android:text="微信"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <ImageView
            android:id="@+id/add"
            android:layout_alignParentRight="true"
            android:layout_marginRight="30dp"
            android:layout_marginTop="11dp"
            android:src="@drawable/mskk"
            android:layout_width="30dp"
            android:layout_height="30dp" />
    </RelativeLayout>

</LinearLayout>



<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/blue" android:title="蓝色" app:showAsAction="never" ></item>
    <item android:id="@+id/red" android:title="红色" > </item>
    <item android:id="@+id/green" android:title="绿色" ></item>
</menu>





public class Main2Activity extends AppCompatActivity {
    private TextView text;

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

        text = (TextView) findViewById(R.id.text);

        registerForContextMenu(text);
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.ceshi, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.blue:
                text.setTextColor(Color.parseColor("#2239A2"));
                break;
            case R.id.green:
                text.setTextColor(Color.parseColor("#1BA233"));
                break;
            case R.id.red:
                text.setTextColor(Color.parseColor("#A21C31"));
                break;
        }
        return super.onContextItemSelected(item);
    }
}




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        />
</LinearLayout>

 类似资料: