【问题标题】:Positioning a scrollview within a popup window在弹出窗口中定位滚动视图
【发布时间】:2021-09-10 20:17:08
【问题描述】:

我正在开发我的第一个大型 android 项目,但我遇到了一个问题。 我必须显示一个带有条款和条件文本的弹出窗口。由于它的大小,文本必须滚动。我的具体问题是,出于视觉原因,我希望顶部位于屏幕下方,而我所做的任何事情都不会让它从最顶部移动。

xml 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <ScrollView
        android:id="@+id/tc_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_long"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="A very long string of text"></TextView>

        </LinearLayout>
    </ScrollView>
</LinearLayout>

我已经尝试添加:

tools:layout_editor_absoluteY="100dp" />

android:layout_marginTop="100dip"

在各个地方 - 滚动视图本身、父 LinearLayout 等,但没有任何作用。

我已经查看了显示窗口的代码,并尝试在 ScrollView 类中使用 scrollTo 方法,如下所示:

 View tcView = inflater.inflate(R.layout.t_and_c_dialog, null);
        ScrollView tcScroll = (ScrollView) tcView.findViewById(R.id.tc_scroll);
        tcScroll.scrollTo(0, 200);
        PopupWindow pwTC = new PopupWindow(tcView, width, width, true);
        pwTC.showAtLocation(new LinearLayout(this), Gravity.CENTER, 0, 0);

仍然没有喜悦。有人有什么想法吗? 非常感谢 托尼·雷诺兹

【问题讨论】:

    标签: android scrollview


    【解决方案1】:

    我继续尝试各种事情并想出了以下内容,虽然我并不完全理解但很实用。 首先,使用以下代码弹出弹出窗口:

    View tcView = inflater.inflate(R.layout.t_and_c_dialog, null);
    PopupWindow pwTC = new PopupWindow(tcView, width, width, true);
    pwTC.showAtLocation(new LinearLayout(this), Gravity.NO_GRAVITY, 0, 800);
    

    这会将窗口放置在硬连线的 y 坐标上。

    xml 现在是:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:padding="10dip"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ScrollView
            android:id="@+id/tc_scroll"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:scrollbars="none" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
    
                <TextView
                    android:id="@+id/tv_long"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
    
                    android:text="A very long string of text"></TextView>
            </LinearLayout>
    </ScrollView>
    
    </LinearLayout>
    

    所以设置一个固定的窗口深度。这可以正常工作,如下所示。

    它只为给定的模拟器提供正确的外观,所以我必须弄清楚如何以编程方式设置大小。

    我有义务提供任何关于这里到底发生了什么和/或改进提示的 cmets

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      相关资源
      最近更新 更多