首页 > 解决方案 > 无法使小部件可滚动

问题描述

我想创建一个图像按钮列表,我添加了 3 个,但只有两个可见,我想让它们可滚动。我尝试使用 ScrollView 但它仍然无法正常工作。我还尝试用 ScrollView 替换第一行中的 RelativeLayout,但应用程序开始崩溃。

这是xml文件:

 <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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back"
tools:context=".home">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/download"
            android:layout_gravity="center_horizontal"
            />

    </RelativeLayout>

</ScrollView>

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="230dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/geo" />
    </RelativeLayout>

</ScrollView>

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="4300dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/images"
            android:contentDescription="TODO" />

    </RelativeLayout>

</ScrollView>

标签: androidlayoutwidgetscrollviewimagebutton

解决方案


将第一个滚动视图的宽度和高度设置为包装内容。如果它不起作用,则只制作一个滚动视图。然后将线性布局作为其子级,并在具有垂直方向的线性布局中添加 3 个图像按钮。


推荐阅读