【问题标题】:How to make the following Android activity scrollable when ScrollView isn't working?当 ScrollView 不工作时,如何使以下 Android 活动可滚动?
【发布时间】:2016-12-26 01:11:25
【问题描述】:

我有以下活动。

我尝试用ScrollView 封装整个RelativeLayout。但是,它不起作用,只显示了最后 2 张图像。所以,我删除了它并发布了有问题的RelativeLayout。请帮忙。我有 2 个虚拟视图,它们在屏幕中心标记一条假想的垂直和水平线,并且所有图像都相对于这些视图对齐。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/content_front"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorWhite"
        android:fillViewport="true"
        android:gravity="center_vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".FrontActivity"
        tools:showIn="@layout/activity_front"
        >

        <View
            android:id="@+id/dummy"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />

        <View
            android:id="@+id/dummy2"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />

        <ImageView
            android:id="@+id/all1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_above="@+id/part2"
            android:layout_margin="20dp"
            android:layout_toLeftOf="@+id/dummy"
            android:layout_toStartOf="@+id/dummy"
            android:adjustViewBounds="false"
            android:contentDescription="@string/app_name"
            android:scaleType="fitCenter"
            android:src="@mipmap/all" />

        <ImageView
            android:id="@+id/part1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_above="@+id/part3"
            android:layout_margin="20dp"
            android:layout_toEndOf="@+id/dummy"
            android:layout_toRightOf="@+id/dummy"
            android:adjustViewBounds="false"
            android:contentDescription="@string/app_name"
            android:scaleType="fitCenter"
            android:src="@mipmap/part1" />

        <ImageView
            android:id="@+id/part2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_above="@+id/dummy2"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:layout_toLeftOf="@+id/dummy"
            android:layout_toStartOf="@+id/dummy"
            android:adjustViewBounds="false"
            android:contentDescription="@string/app_name"
            android:scaleType="fitCenter"
            android:src="@mipmap/part2" />

        <ImageView
            android:id="@+id/part3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_above="@+id/dummy2"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:layout_toEndOf="@+id/dummy"
            android:layout_toRightOf="@+id/dummy"
            android:adjustViewBounds="false"
            android:contentDescription="@string/app_name"
            android:scaleType="fitCenter"
            android:src="@mipmap/part3" />

        <ImageView
            android:id="@+id/part4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/dummy2"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="40dp"
            android:layout_toLeftOf="@+id/dummy"
            android:layout_toStartOf="@+id/dummy"
            android:adjustViewBounds="false"
            android:contentDescription="@string/app_name"
            android:scaleType="fitCenter"
            android:src="@mipmap/part4" />

        <ImageView
            android:id="@+id/part5"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/dummy2"
            android:layout_marginLeft="20dp"
            android:layout_marginStart="20dp"
            android:layout_marginTop="40dp"
            android:layout_toEndOf="@+id/dummy"
            android:layout_toRightOf="@+id/dummy"
            android:adjustViewBounds="false"
            android:contentDescription="@string/app_name"
            android:scaleType="fitCenter"
            android:src="@mipmap/part5" />
    </RelativeLayout>

任何关于如何提高效率的提示也将不胜感激,谢谢。

【问题讨论】:

    标签: java android xml android-relativelayout android-scrollview


    【解决方案1】:

    第一个提示:当您想要在布局中只包含空格的虚拟视图时,请使用 Space 视图。

    第二个提示:if you're supporting API 17 and above,包括对 Start 和 End 限定符的支持,而不是(只是,取决于您的 minSdk)Left 和 Right。


    也就是说,您正在做的事情可以通过TableLayout 或GridLayout 来完成,而无需使用虚假的锚点视图。它仍然可以在没有它们的情况下使用RelativeLayout,只需根据[0,0],[1,1]到[1]相对于[0,0],[1,0]对齐[0,1]的位置,0] 和 [0,1] 等等。

    您没有完全指定您的要求,因此我将冒昧使用 GridLayout,因为代码简单且灵活,可以适应未来不同的配置/需求(我也怀疑 它比相对于其他视图对齐位置要便宜,但您必须自己检查)。这里我不包括 Left 和 Right 限定符,因为无论如何我可能会将 minSdk 设置为 17:

    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingStart="@dimen/activity_horizontal_margin"
        android:paddingEnd="@dimen/activity_horizontal_margin"
        android:columnCount="2"
        android:layout_gravity="center_horizontal">
        <!-- Set columnCount according to your needs. -->
    
        <ImageView
            android:id="@+id/image_1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:adjustViewBounds="false"
            android:layout_margin="20dp"
            android:contentDescription="@string/app_name"
            android:scaleType="fitCenter"
            android:src="@mipmap/icon_1" />
    
        <ImageView
            android:id="@+id/image_2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:adjustViewBounds="false"
            android:layout_margin="20dp"
            android:contentDescription="@string/app_name"
            android:scaleType="fitCenter"
            android:src="@mipmap/icon_2" />
    
    <!-- Rinse and repeat. -->
    
    </GridLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多