PopupWindow基本使用方法


public class MainActivity extends Activity implements OnClickListener {



    private Context context = this;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button bt = (Button) findViewById(R.id.bt);

        bt.setOnClickListener(this);

    }



    @Override

    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.bt:

            showPopWindow(v);

            break;

        case R.id.layoutSeclect1:

            Toast.makeText(context, "模式1", Toast.LENGTH_SHORT).show();

            break;

        case R.id.layoutSeclect2:

            Toast.makeText(context, "模式2", Toast.LENGTH_SHORT).show();

            break;

        }

    }



    private void showPopWindow(View v) {

        // 一、把一个自定义的布局打气,作为popwindow要显示的内容

        View contentView = LayoutInflater.from(context).inflate(

                R.layout.pop_layout, null);

        // 二、找到该布局中的控件

        LinearLayout layoutSeclect1 = (LinearLayout) contentView

                .findViewById(R.id.layoutSeclect1);

        LinearLayout layoutSeclect2 = (LinearLayout) contentView

                .findViewById(R.id.layoutSeclect2);

        // 三、设置控件的点击事件

        layoutSeclect1.setOnClickListener(this);

        layoutSeclect2.setOnClickListener(this);

        // 四、new出PopupWindow,参数为PopupWindow显示的内容view、宽、高、是否可获取焦点

        PopupWindow popupWindow = new PopupWindow(contentView,

                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);// 不写也行,因为默认为true,可触摸

        // 五、★必须设置背景:如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框

        popupWindow.setBackgroundDrawable(new BitmapDrawable(context

                .getResources()));

        // ——————————————————————————————————————————————————————————————————————————————————————————

        // 六、让PopupWindow显示出来:显示popwindow的方式有两种,看你是想相对于某个控件显示,还是相对于父控件显示

        // 1.showAsDropDown指的是显示在v控件的周围,是以这个控件为基点的

        // popupWindow.showAsDropDown(v);

        // popupWindow.showAsDropDown(v, 0, -30);//这里的坐标是基于附着控件的左下角,下右为正

        // 2.showAtLocation指的是显示在现对于父控件的位置,第一个参数view和相对于控件的一样

        popupWindow.showAtLocation(v, Gravity.CENTER | Gravity.CENTER, 0, 0);



    }



}



布局


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.example.mypopwindowdemo.MainActivity" >



    <Button

        android:id="@+id/bt"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="模式选择" />



</RelativeLayout>



PopupWindow布局


<?xml version="1.0" encoding="UTF-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#0000" >



    <!--

  popupwimdow的布局注意不能使用类似        

    android:layout_marginTop=""这样属性,特别是相对布局时候. 

  布局或者控件整体靠左等都会影响popupwimdow定位使用

    -->



    <LinearLayout

        android:id="@+id/hot_layout"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:background="@drawable/pop"

        android:orientation="vertical" >



        <LinearLayout

            android:id="@+id/layoutSeclect1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:clickable="true"

            android:onClick="OnclickTestListener" >



            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:padding="8dp"

                android:text="回家的方案1"

                android:textColor="#fff"

                android:textSize="18sp" />

        </LinearLayout>



        <LinearLayout

            android:id="@+id/layoutSeclect2"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:clickable="true"

            android:onClick="OnclickTestListener" >



            <TextView
引用\[1\]:遇到问题: FileNotFoundError: \[Errno 2\] No such file or directory: 'models/trained_models/wind_model_NL_1h_CNN2DWind_NL.pt' 。 引用\[2\]:报错代码 #FileNotFoundError: \[Errno 2\] No such file or directory: './data/articles.csv' aaa = pd.read_csv('./data/articles.csv') # ./data表示该目录下有个data文件,实际data是在code文件的上一层文件中 aaa 。 引用\[3\]:** 代码如下 import csv import os import pandas as pd # 先创建一个空列表,然后通过for循环实现 readList = \[\] files = os.listdir(r"D:\Lynn.Yan\Python\Plot\data") # i是文件 # 读取多个csv文件内容 # 跳过第一行“标题” # 添加“文件名”作为新的列 for i in files: with open('{}'.format(i), 'r') as read_csvfile: read_all = csv.reader(read_csvfile) next(read_csvfile) for line in read_all: line.append(i) readList.append(line) # list转化成dataframe # 添加列名 testData = pd.DataFrame(readList) titleList = \["Cycle", "Voltage", "Current", "RPM", "Torque", "Force", "Position", "CustomAI", "TC1", "TC2", "TC3", "TC4", "TC5", "UpdateTime", "Filename"\] testData.columns = titleList testData.head() 。 问题:FileNotFoundError: \[Errno 2\] No such file or directory: 'Results/time_redact.csv' 回答: 这个错误是由于文件或目录不存在导致的。在你的代码中,你尝试读取了一个名为'models/trained_models/wind_model_NL_1h_CNN2DWind_NL.pt'的文件,但是该文件不存在。同样地,在另一个代码段中,你尝试读取了一个名为'./data/articles.csv'的文件,但是该文件也不存在。最后,你还尝试读取了一个名为'Results/time_redact.csv'的文件,同样地,该文件也不存在。请确保你提供的文件路径是正确的,并且文件确实存在于指定的位置。 #### 引用[.reference_title] - *1* [FileNotFoundError: [Errno 2] No such file or directory: ‘models/trained_models/wind_model_NL_1h_CNN...](https://blog.youkuaiyun.com/lcnana/article/details/121307592)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [FileNotFoundError: [Errno 2] No such file or directory: ‘./data/articles.csv](https://blog.youkuaiyun.com/weixin_43409127/article/details/124175494)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [解决FileNotFoundError: [Errno 2] No such file or directory: ‘220102933.86_56.csv](https://blog.youkuaiyun.com/weixin_45560266/article/details/126460377)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值