【问题标题】:Android Scroll View Popup Not workingAndroid滚动视图弹出窗口不起作用
【发布时间】:2016-04-08 21:25:44
【问题描述】:

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

如果我制作滚动视图在我的视图中显示错误ScrollView 只能承载一个直接子级(详情)

这是我的代码

   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 并将 ID(@+id/popup_element) 分配给该父布局,而不是将 ID 分配给 ScrollView。并确保您的 ScrollView 仅包含一个子布局。因此,将您的其他布局合并到单个 LinearLayout 中,并将该布局作为 ScrollView 的子级!

标签: android xml android-studio scrollview


【解决方案1】:

ScrollView 正在处理PopupWindow

我的问题是因为popup的窗口高度没有限制

您可以通过View.measure(int widthMeasureSpec, int heightMeasureSpec)的方法预先确定布局的高度,但不能无限制,高度小于屏幕或窗口会滚动

【讨论】:

    【解决方案2】:

    以线性布局为父:

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

    【讨论】:

    • schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 作为父级 。 . . .
    • 将滚动视图高度设置为 wrap_content。
    【解决方案3】:

    这可能对你有帮助...

    <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 添加为直接子级并将其他控件放入其中。

    【讨论】:

      【解决方案4】:

      在 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 并且还可以减少弹出窗口的宽度和高度

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

      【讨论】:

      • ScrollView 只能承载一个直接子节点如果您使用我的解决方案,则不应出现此错误
      【解决方案5】:

      下面你应该这样做!

      <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 应该在 ScrollView 内
      • @Kaushik 我试过了兄弟..但它也没有帮助我......!!
      • 好吧,然后更新你的问题......你尝试了什么
      猜你喜欢
      • 2016-12-25
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多