【问题标题】:How would I use a ScrollView inside a FrameLayout?我将如何在 FrameLayout 中使用 ScrollView?
【发布时间】:2015-06-16 15:05:34
【问题描述】:

让我的 ScrollView 正常运行时遇到了很多麻烦。我正在使用主细节设计。大师滚动,因为它是一个列表视图。当 Activity 在消失之前被充气时,选择的 Detail 将显示在我的 XML 中实现的滚动条几秒钟(Stackoverflow 不允许我发布错误的屏幕截图,因为这是我的第一篇帖子)

当我尝试使用触摸输入滚动 Detail Activity 时,没有任何反应。我尝试将 Mel 的解决方案复制并粘贴到 ScrollView doesn't work 的帖子中看似相同的问题。对于父级、子级和 ScrollView 本身,我还尝试了所有可能的 match_parent 和 fill_parent 组合。任何帮助表示赞赏。我的 XML 代码如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fillViewport="true"
        >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/linearLayout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp"
            android:layout_marginTop="20dp"
            android:stretchColumns="1, 2"
            >

            <ImageView
                android:id="@+id/picture"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:src="@drawable/bmp1"
                android:layout_alignParentTop="true"
                />

            <TableLayout
                android:id="@+id/tableLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:layout_marginTop="20dp"
                android:stretchColumns="1, 2">

                     <!-- Table Row 0 -->
                     <TableRow
                         android:layout_height="wrap_content"
                         android:layout_width="match_parent"
                         android:id="@+id/tableRow0">
                         <TextView
                             android:id="@+id/priWeapon"
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:text="@string/priWeapon" android:textColor="#000" android:textStyle="bold"
                             android:gravity="left"
                             android:padding="5dp"/>
                         <EditText
                             android:id="@+id/priWeaponEditText"
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:text="@string/NA" android:textSize="14sp" android:textStyle="bold"
                             android:gravity="center"
                             android:focusable="false"
                             android:layout_weight="1"
                             android:cursorVisible="false"
                             android:longClickable="false"/>
                     </TableRow>

                     <!-- Table Row 1 -->
                     <TableRow
                         android:layout_height="wrap_content"
                         android:layout_width="match_parent"
                         android:id="@+id/tableRow1">
                         <TextView
                             android:id="@+id/priWeaponRange"
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:text="@string/priMaxRange" android:textColor="#000"
                             android:gravity="left"
                             android:padding="5dp"/>
                         <EditText
                             android:id="@+id/priWeaponRangeEditText"
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:text="@string/NA" android:textSize="14sp"
                             android:gravity="center"
                             android:focusable="false"
                             android:layout_weight="1"
                             android:cursorVisible="false"
                             android:longClickable="false"/>
                     </TableRow>

                </TableLayout>

            </LinearLayout>

        </ScrollView>

</FrameLayout>

【问题讨论】:

    标签: android xml scrollview master-detail android-framelayout


    【解决方案1】:

    我找到了解决办法!我没有尝试将格式应用于 Activity XML 本身,而是将其剪切并粘贴到没有 frameLayout 的 Activity 片段 XML 中。由于此更改,它现在可以完美滚动,同时在整个滚动过程中将标题栏保持在顶部,这正是我想要的行为。我已经在下面发布了最终代码。如果您有一个仍然允许我在 FrameLayout 中使用 scrollView 的解决方案,尽管我很乐意看到它以供将来参考。感谢您的帮助!

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fillViewport="true"
        >
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linearLayout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        android:layout_marginTop="20dp"
        android:stretchColumns="1, 2"
        >
    
        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/item_detail"
            style="?android:attr/textAppearanceLarge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:textIsSelectable="true"
            tools:context=".ItemDetailFragment"/>
    
        <ImageView
            android:id="@+id/picture"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:src="@drawable/bmp1"
            android:layout_alignParentTop="true"
            />
    
        <TableLayout
    
            android:id="@+id/tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:layout_marginTop="20dp"
            android:stretchColumns="1, 2">
    
            <!-- Table Row 0 -->
            <TableRow
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:id="@+id/tableRow0">
                <TextView
                    android:id="@+id/priWeapon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/priWeapon" android:textColor="#000" android:textStyle="bold"
                    android:gravity="left"
                    android:padding="5dp"/>
                <EditText
                    android:id="@+id/priWeaponEditText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/NA" android:textSize="14sp" android:textStyle="bold"
                    android:gravity="center"
                    android:focusable="false"
                    android:layout_weight="1"
                    android:cursorVisible="false"
                    android:longClickable="false"/>
            </TableRow>
        </TableLayout>
    
    </LinearLayout>
    
    </ScrollView>

    【讨论】:

      【解决方案2】:

      您的目标是使用ImageView 的最大屏幕尺寸,然后向下滚动到(起初不可见)TableLayout?那是不可能的。当您在 ImageView 上设置 android:layout_height="fill_parent" 时,它会使用最大可能(在您的示例中为所有屏幕尺寸)空间。所以没有TableLayout 被渲染,也不需要滚动。您应该在 ImageView 中设置android:layout_height="wrap_content"

      源代码的一些注释:

      • 使用match_parent 代替fill_parent。自 API 级别 8 起已重命名
      • TableLayout 中有一个无用的android:layout_below="@id/picture"。可以使用RelativeLayout 获得以前版本的工件吗?
      • 删除TableLayout 中的命名空间定义

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-15
        • 2021-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-31
        相关资源
        最近更新 更多