• PopupWindow弹出窗口的使用


    1.主布局文件activity_popup_window.xml:

    <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.androidui.PopupWindowActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="14dp"
            android:text="Button" />
    
    </RelativeLayout>

    2.弹出窗口的布局文件view_popup_window.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" >
    
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >
    
            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="苏州" />
    
            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="上海" />
    
            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="北京" />
        </RadioGroup>
    
    </LinearLayout>

    3.Activity代码:

    public class PopupWindowActivity extends Activity {
    
        private PopupWindow popupWindow;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_popup_window);
            View view = getLayoutInflater().inflate(R.layout.view_popup_window, null);
            popupWindow = new PopupWindow(view, 200, 400);
            // 加载对应layout文件中的组件
            RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    Toast.makeText(PopupWindowActivity.this, "click" + checkedId, Toast.LENGTH_LONG).show();
                }
            });
            Button button = (Button) findViewById(R.id.button1);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (popupWindow.isShowing()) {
                        popupWindow.dismiss();
                    } else {
                        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, -10);
                    }
                }
            });
        }
    
    }

    #遇到的问题:

    使用findViewById(R.id.radioGroup1)时导致程序崩溃。

    #原因:

    使用findViewById(R.id.radioGroup1)时默认调用当前的Layout文件生成的View对象对组件初始化,但在当前Layout布局文件中没有定义R.id.radioGroup1所以会导致程序崩溃。

    #解决办法:

    实例化R.id.radioGroup1所在Layout布局文件:

    LayoutInflater.from(context).inflate(resource, root).findViewById(id)

    用该Layout布局文件的实例加载R.id.radioGroup1组件。

    这也解释了问什么相同Layout文件中不可有相同id,不同Layout文件中可以有相同id的问题,因为不同Layout中相同id的组件是用与其组件相对应的Layout布局文件的实例加载的。

  • 相关阅读:
    SilkTest天龙八部系列1-初始化和构造函数
    SilkTest天龙八部系列3-动态父窗口
    SilkTest天龙八部系列4-ChildWin
    java程序查不出数据来
    关于模拟器不能运行项目问题:Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
    汉语-词语:寂静
    汉语-词语:幽静
    汉语-词语:恬静
    汉语-词语:静谧
    汉语-词语:冷静
  • 原文地址:https://www.cnblogs.com/mada0/p/4829894.html
Copyright © 2020-2023  润新知