Android ScrollView 弹出窗口不工作

标签 android xml android-studio scrollview

我是安卓新手。我正在尝试在 android 上实现可滚动的弹出窗口。

如果我制作 ScrollView 在我看来它显示错误ScrollView can host only an direct child (Details)

这是我的代码

   private PopupWindow pwindo;

    private void initiatePopupWindow() {
        try {
// We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) About.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.screen_popup,
                    (ViewGroup) findViewById(R.id.popup_element));
            pwindo = new PopupWindow(layout,700,1000, true);
           // pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
          //  pwindo.showAsDropDown(layout, 80, 80);

            btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
            btnClosePopup.setOnClickListener(cancel_button_click_listener);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private OnClickListener cancel_button_click_listener = new OnClickListener() {
        public void onClick(View v) {
            pwindo.dismiss();

        }
    };

还有我的看法

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/popup_element">
<TextView/>
<TextView/>
<TextView/>
<ImageButton/>
<ImageButton/>
.
.
.
.


</ScrollView>

提前致谢。

最佳答案

在 ScrollView 中放置一个 LinearLayout 作为 you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects

布局应该是这样的

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/popup_element">
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical">
      <TextView/>
      <TextView/>
      <TextView/>
      <ImageButton/>
      <ImageButton/>
   </LinearLayout>

</ScrollView>

将所有这些 View 放在 LinearLayout 中 并为弹出窗口提供更少的宽度和高度

pwindo = new PopupWindow(layout,400,400, true);

关于Android ScrollView 弹出窗口不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34605164/

相关文章:

java - 按下 WebView 内的按钮不起作用

java - MediaPlayer 在使用 Handler 时出现滞后

xml - WiX - 查找 .msi 目录的父目录

php - 返回 mysql 字段中的 xml 参数

android - 了解有关 Android Studio 中线程的信息

android - 地理围栏转换服务和唤醒锁

xml - PHP DOMNode insertAfter?

android - 错误:未知主机 'jcenter.bintray.com: Name or service not known'。您可能需要在Gradle中调整代理设置

java - 启动 Activity 时 Google map API key 错误

java - 如何处理 Android 中的 3 个硬键