Android 滚动视图弹出窗口不工作

Android Scroll View Popup Not working

我是 android 的新手。我正在尝试在 android.

上实现可滚动弹出窗口 window

如果我制作滚动视图在我看来它显示错误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>

提前致谢。

以线性布局为父:

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

<TextView/> <TextView/> <TextView/> <ImageButton/> <ImageButton/> . . . .

</ScrollView> 
</linearLayout>

下面你应该这样做!

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"

    <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>
</LinearLayout>

在 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>

将所有这些视图放入 LinearLayout 并为弹出 window

提供更少的宽度和高度
pwindo = new PopupWindow(layout,400,400, true);

这可能对您有所帮助...

<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:id="@+id/child">
    <TextView/>
    <TextView/>
    <TextView/>
    <ImageButton/>
    <ImageButton/>
  .
  .
  .
  .

 </LinearLayout>
 </ScrollView>

将 LinearLayout 添加为直接子控件,并将其他控件放入其中。

ScrollView 正在 PopupWindow!

我的问题是window弹窗的高度没有限制

可以通过方法View.measure(int widthMeasureSpec, int heightMeasureSpec)预先确定layout的高度,但不能无限制,高度小于screen或者window就会有滚动