安卓 菜单和弹窗

=@[toc](安卓 菜单和弹窗)

菜单

系统菜单(OptionsMenu)

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

<?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/big" android:title="大力" app:showAsAction="always"> </item>
    <item android:id="@+id/dog" android:title="成果" app:showAsAction="ifRoom"> </item>
</menu>

Activity重写onCreateOptionsMenu加载资源文件

//系统菜单 OptionsMenu
    //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.big:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
                
            case R.id.dog:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

上下文菜单(ContextMenu)

和上面步骤一样但是增加了一个添加长按属性并将菜单绑定到控件上

 private TextView tvShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvShow = (TextView) findViewById(R.id.tv_show);
        //给控件添加长按
        registerForContextMenu(tvShow);
    }
//上下文菜单ContextMenu
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.menu,menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.big:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;

            case R.id.dog:
                Toast.makeText(this, ""+item.getTitle(), Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

弹出菜单

private TextView tvXian;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tvXian = (TextView) findViewById(R.id.tv_xian);
        //点击事件
        tvXian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupMenu();
            }
        });
    }

    public void showPopupMenu(){
        //创建对象
        //参数一 上下文  参数二  菜单显示在指定控件的下方
        PopupMenu popupMenu = new PopupMenu(this,tvXian);
        //记载布局
        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.big:
                        //点击是文本框文字变颜色
                        tvXian.setTextColor(Color.parseColor("#000999"));
                        break;
                    case R.id.dog:
                        tvXian.setTextColor(Color.parseColor("#999111"));
                        break;
                }
                return false;
            }
        });
        //显示
        popupMenu.show();
    }

弹出窗口

布局文件

<?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=".Main3Activity">

    <TextView
        android:id="@+id/tv_xin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="弹出窗口"
        android:textSize="30sp"
        />
</LinearLayout>

java代码

public class Main3Activity extends AppCompatActivity {
    private TextView tvXin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main3);
        tvXin = (TextView) findViewById(R.id.tv_xin);
        tvXin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建对象
                PopupWindow popupWindow = new PopupWindow(Main3Activity.this);
                //加载布局
                View view = LayoutInflater.from(Main3Activity.this).inflate(R.layout.tan_layout, null);
                //设置三要素,缺一不可.
                popupWindow.setContentView(view);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setWidth(600);
                //获取聚焦
                popupWindow.setFocusable(true);
                //点击外部取消
                popupWindow.setOutsideTouchable(true);
                //设置弹出位置
                popupWindow.showAsDropDown(tvXin,200,200);//x轴y轴偏移距离
            }
        });
    }
}

弹出的布局

<?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="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的大力"
        android:textSize="20sp"
        />
    <ImageView
        android:layout_width="80dp"
        android:layout_height="100dp"
        android:src="@drawable/a8"
        />
</LinearLayout>

进出场动画

在res下新建文件夹anim放置进出场动画的xml文件
进场动画的xml文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    >
    <!-- duration是持续的时间  -->
    <!-- translate是向某个方向移动  xy轴  -->
    <translate android:fromYDelta="-300" android:toYDelta="0"> </translate>
    <!-- 是透明的变化  -->
    <alpha android:fromAlpha="0.1" android:toAlpha="1"> </alpha>
</set>

出场动画的xml文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    >
    <alpha android:fromAlpha="1" android:toAlpha="0.1"> </alpha>
    <translate android:fromYDelta="0" android:toYDelta="300"> </translate>
</set>

修改values文件夹下的styles文件

<style name="poppu_windio" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/popup_in</item>
        <item name="android:windowExitAnimation">@anim/popup_out</item>
    </style>

java文件中应用

//进场出场动画
popupWindow.setAnimationStyle(R.style.poppu_windio);

进出场透明度

java代码

				//设置透明度
                final 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 = 1.0f;
                        getWindow().setAttributes(attributes1);
                    }
                });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值