Andriod studio 学习 之 Menu菜单与PopupWindow弹出窗体

39 篇文章 0 订阅
34 篇文章 0 订阅

Menu菜单与PopupWindow弹出窗体

Menu菜单

常用的菜单
1.系统菜单OptionsMenu
在res下面创建一个menu文件夹,并新建一个xml文件作为OptionMenu的布局文件
	

<menu xmlns:android="http://schemas.android.com/apk/res/android">
	    <item android:id="@+id/blue" android:title="蓝色"></item>
	    <item android:id="@+id/red" android:title="红色"></item>
	    <item android:id="@+id/other" android:title="其他颜色">
	        <menu>
	            <item android:id="@+id/white" android:title="白色"></item>
	
	            <item android:id="@+id/green" android:title="绿色"></item>
	
	        </menu>
	    </item>
	</menu>

Activity重写onCreateOptionsMenu加载资源文件
Activity重写onOptionsItemSelected加设置事件监听

 //Activity重写onCreateOptionsMenu加载资源文件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    //Activity重写onOptionsItemSelected设置事件监听
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.blue:
                Toast.makeText(this, "蓝色", Toast.LENGTH_SHORT).show();
            break;
            case R.id.red:
                Toast.makeText(this, "红色", Toast.LENGTH_SHORT).show();
                break;
            case R.id.white:
                Toast.makeText(this,item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.green:
                Toast.makeText(this,item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

步骤流程:

2.上下文菜单ContextMenu

在res下面创建一个menu文件夹,新建一个xml文件作为ContextMenu的布局文件,复用上面的menu布局

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.menu,menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        int itemId = item.getItemId();
        switch (itemId){
            case R.id.blue:
                Toast.makeText(this, "蓝色", Toast.LENGTH_SHORT).show();
                break;
            case R.id.red:
                Toast.makeText(this, "红色", Toast.LENGTH_SHORT).show();
                break;
            case R.id.white:
                Toast.makeText(this,item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.green:
                Toast.makeText(this,item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }

        return super.onContextItemSelected(item);
    }
3.弹出菜单

在res下面创建一个menu文件夹,并新建一个xml文件作为PoupMenu的布局文件。

<?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"
    android:gravity="center">
    <TextView
        android:id="@+id/popup_tv"
        android:text="弹出菜单"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    text = (TextView) findViewById(R.id.textid);
    text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showPopupMenu();
        }
    });
}

 public void showPopupMenu(){
        //参数一 上下文  参数二  菜单显示在指定控件的下方
        PopupMenu popupMenu = new PopupMenu(this, text);
        //记载布局
        popupMenu.inflate(R.menu.menu);
        //事件监听
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int itemId = item.getItemId();
                switch (itemId){
                    case R.id.blue:
                        Toast.makeText(Main3Activity.this, "蓝色", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.red:
                        Toast.makeText(Main3Activity.this, "红色", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.white:
                        Toast.makeText(Main3Activity.this,item.getTitle(), Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.green:
                        Toast.makeText(Main3Activity.this,item.getTitle(), Toast.LENGTH_SHORT).show();
                        break;
                }
                return false;
            }
        });
        popupMenu.show();
    }

PopupWindow弹出窗体

PopupWindow

一.PopupWindow介绍

PopupWindow弹出窗体可以在任意位置弹出窗体,而对话框只能出现屏幕最中间。

二.如何自定义窗体

(1)构造方法:public PopupWindow (Context context):context上下文对象
(2)必须设置的3大要素:
setContentView():设置自定义布局
setWidth():设置宽度
setHeight():设置高度
(3)显示窗体:
a。显示在某个指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);//xoff和yoff都是偏移量
b。指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);
//gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT

三.实现微信QQ支付宝右上角加号弹出窗体

(1)xml布局文件

<?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"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:background="#393A3F"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        >
        <TextView
            android:gravity="center_vertical"
            android:textColor="#fff"
            android:textSize="30sp"
            android:text="微信"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <ImageView
            android:layout_alignParentRight="true"
            android:layout_marginRight="50dp"
            android:src="@drawable/search"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <ImageView
            android:id="@+id/add"
            android:layout_alignParentRight="true"
            android:src="@drawable/add"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </RelativeLayout>

   <ListView
       android:id="@+id/lv"
       android:layout_weight="8"
       android:layout_width="match_parent"
       android:layout_height="0dp"></ListView>

   <RadioGroup
       android:layout_weight="1"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:orientation="horizontal">
       <RadioButton
           android:textAlignment="center"
           android:drawableTop="@drawable/selector1"
           android:button="@null"
           android:textSize="20sp"
           android:text="微信"
           android:textColor="@drawable/selector2"
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="match_parent" />
       <RadioButton
           android:textAlignment="center"
           android:drawableTop="@drawable/selector1"
           android:button="@null"
           android:textSize="20sp"
           android:text="微信"
           android:textColor="@drawable/selector2"
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="match_parent" />
       <RadioButton
           android:textAlignment="center"
           android:drawableTop="@drawable/selector1"
           android:button="@null"
           android:textSize="20sp"
           android:text="微信"
           android:textColor="@drawable/selector2"
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="match_parent" />
       <RadioButton
           android:textAlignment="center"
           android:drawableTop="@drawable/selector1"
           android:button="@null"
           android:textSize="20sp"
           android:text="微信"
           android:textColor="@drawable/selector2"
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="match_parent" />
   </RadioGroup>
</LinearLayout>
----------------------------------

自定义弹出窗体布局xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#45494A"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:src="@drawable/chat"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
        <TextView
            android:textColor="#fff"
            android:textSize="20sp"
            android:text="发起群聊"
            android:layout_weight="7"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#fff">
        
    </View>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:src="@drawable/chat"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
        <TextView
            android:textColor="#fff"
            android:textSize="20sp"
            android:text="发起群聊"
            android:layout_weight="7"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#fff">
    </View>
</LinearLayout>


------------------------------------------------


Java代码:
 private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //TODO 去除自带的bar
    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main2);
    imageView = (ImageView) findViewById(R.id.add);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            show_popupwindow();
        }
    });
}
//弹出窗体
public void show_popupwindow(){
    //TODO 1:实例化对象
    PopupWindow popupWindow = new PopupWindow(Main2Activity.this);
    //TODO 2:设置属性
    View view= LayoutInflater.from(this).inflate(R.layout.layout_weixin_popupwindow,null);
    popupWindow.setContentView(view);
    popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    popupWindow.setWidth(600);
    //设置点击外部消失
    popupWindow.setOutsideTouchable(true);
    //TODO 3:展示
    popupWindow.showAsDropDown(imageView,-100,-100);
}

四.底部弹出窗体

(1)xml布局文件:activity_main.xml

<?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"
    android:gravity="center">
    <Button
        android:onClick="click"
        android:text="弹出窗体"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

(2)弹出窗体xml布局文件:layout3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#F0E8F0"
    android:orientation="vertical">
    <TextView
        android:id="@+id/camera_tv"
    android:textAlignment="center"
    android:textSize="30sp"
    android:text="拍照"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <View
        android:background="#969696"
        android:layout_width="match_parent"
        android:layout_height="5dp"></View>

    <TextView
        android:textAlignment="center"
        android:textSize="30sp"
        android:text="从手机相册选择"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <View
        android:background="#969696"
        android:layout_width="match_parent"
        android:layout_height="5dp"></View>
    <TextView
        android:textAlignment="center"
        android:textSize="30sp"
        android:text="保存图片"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <View
        android:background="#969696"
        android:layout_width="match_parent"
        android:layout_height="15dp"></View>
    <TextView
        android:textAlignment="center"
        android:textSize="30sp"
        android:text="取消"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

(3)java 代码:MainActivity.java

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

    public void click(View view) {
        show_popupwindow();
    }
    //弹出窗体
    public void show_popupwindow(){
        //TODO 1:实例化对象
        PopupWindow popupWindow = new PopupWindow(MainActivity.this);
        //TODO 2:设置属性
        View view=LayoutInflater.from(this).inflate(R.layout.layout3,null);
        final TextView textView = view.findViewById(R.id.camera_tv);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, textView.getText().toString().trim(), Toast.LENGTH_SHORT).show();
            }
        });
	popupWindow.setContentView(view);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置点击外部消失
        popupWindow.setOutsideTouchable(true);
      	//TODO 3:展示
        /**
         * @param parent 父布局
         * @param gravity gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT。。。。
         * @param x x轴偏移量
         * @param y y轴偏移量
         */
        View parent=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM,0,0);
    }

五.弹出窗体背景半透明

PopupWindow popupWindow= new PopupWindow(Main3Activity.this);
popupWindow.setHeight(300);
popupWindow.setWidth(400);
View view1=LayoutInflater.from(Main3Activity.this).inflate(R.layout.pop,null);
popupWindow.setContentView(view1);
//设置点击外部,窗体消失
popupWindow.setOutsideTouchable(true);
//1.获得当前窗体的属性
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
//2.设置透明度为0.5f,半透明状态
layoutParams.alpha=0.5f;
//3.为窗体设置新属性
getWindow().setAttributes(layoutParams);
//4.当窗体消失的时候,恢复透明度
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.alpha=1f;
getWindow().setAttributes(layoutParams);
}
});

六.设置动画

1.在res/anim文件夹下定义进场动画
duration:动画时长

<translate 
    android:duration="2000" 
    android:fromYDelta="0" 
    android:toYDelta="100%" /> 


<translate 
    android:duration="2000" 
    android:fromYDelta="100%" 
    android:toYDelta="0" /> 

2.定义进出场动画

<style name="MyPopupWindow">
        <item name="android:windowEnterAnimation">@anim/pop_in</item>
        <item name="android:windowExitAnimation">@anim/pop_out</item>
    </style>
3.为popupwindow设置动画

setAnimationStyle(R.style.MyPopupWindow);

七.自定义PopupWindow
public class MyPopupWindow extends PopupWindow {

    Context mContext;
    private  LayoutInflater mInflater;
    private  View mContentView;


    public MyPopupWindow(Context context) {
        super(context);

        this.mContext=context;
        //打气筒
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        //打气

        mContentView = mInflater.inflate(R.layout.layout_dialog,null);

        //设置View
        setContentView(mContentView);


        //设置宽与高
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);

        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);


        /**
         * 设置进出动画
         */
        setAnimationStyle(R.style.MyPopupWindow);


        /**
         * 设置背景只有设置了这个才可以点击外边和BACK消失
         */
        setBackgroundDrawable(new ColorDrawable());


        /**
         * 设置可以获取集点
         */
        setFocusable(true);

        /**
         * 设置点击外边可以消失
         */
        setOutsideTouchable(true);

        /**
         *设置可以触摸
         */
        setTouchable(true);


        /**
         * 设置点击外部可以消失
         */

        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                /**
                 * 判断是不是点击了外部
                 */
                if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
                    return true;
                }
                //不是点击外部
                return false;
            }
        });


        /**
         * 初始化View与监听器
         */
        initView();

        initListener();
    }




    private void initView() {

    }

    private void initListener() {

    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值