Android第二十二天PopupWindow练习

题目:

创建一个项目,里面有一个按钮,点击按钮弹出popwindow,高150 200popwindow中有3个按钮,点击按钮 改变背景颜色

代码:

布局文件:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.mrzhao.examdemo.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onBtClick"
        android:text="点击弹出PopupWindow" />

</LinearLayout>

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

    <!--50px是因为 题目要求高度是150  所以平均下来每个占据 50px-->
    <Button
        android:id="@+id/bt_red"
        android:layout_width="match_parent"
        android:layout_height="50px"
        android:text="红色" />

    <Button
        android:id="@+id/bt_blue"
        android:layout_width="match_parent"
        android:layout_height="50px"
        android:text="蓝色" />

    <Button
        android:id="@+id/bt_green"
        android:layout_width="match_parent"
        android:layout_height="50px"
        android:text="绿色" />
</LinearLayout>

MainActivity.java:

package com.mrzhao.examdemo;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private PopupWindow popupWindow;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化根视图
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    }

    public void onBtClick(View view) {
        //点击弹出PopupWindow
        View popView = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);
        //实例化菜单中的按钮
        Button redBt = (Button) popView.findViewById(R.id.bt_red);
        Button blueBt = (Button) popView.findViewById(R.id.bt_blue);
        Button greenBt = (Button) popView.findViewById(R.id.bt_green);
        //设置监听事件
        redBt.setOnClickListener(this);
        blueBt.setOnClickListener(this);
        greenBt.setOnClickListener(this);

        //实例化 弹出框
        popupWindow = new PopupWindow(popView,200,150);
        //显示出来
        popupWindow.showAsDropDown(view);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_red:
                //修改背景颜色
                linearLayout.setBackgroundColor(Color.RED);
                break;
            case R.id.bt_blue:
                //修改背景颜色
                linearLayout.setBackgroundColor(Color.BLUE);
                break;
            case R.id.bt_green:
                //修改背景颜色
                linearLayout.setBackgroundColor(Color.GREEN);
                break;
        }
        //弹出框消失
        popupWindow.dismiss();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值