ScrollView无法与ImageButtons一起正常工作

时间:2020-03-04 09:34:11

标签: android scrollview

我想在我的主页中有几个ImageButton,因此应该将其放在ScrollView中,以允许用户滚动浏览所有的它们。但是,它没有按预期工作。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    tools:context=".Activities.HomeFragment">

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

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="216dp"
            android:background="@drawable/lunchimg" />

        <ImageButton
            android:id="@+id/lunch"
            android:layout_width="wrap_content"
            android:layout_height="216dp"
            android:background="@drawable/lunchcate" />

        <ImageButton
            android:id="@+id/dinner"
            android:layout_width="wrap_content"
            android:layout_height="216dp"
            android:background="@drawable/healthyfood" />

    </LinearLayout>
</ScrollView>

当我运行代码时,仅第一个按钮出现,而其他按钮出于某种原因在它后面。

3 个答案:

答案 0 :(得分:1)

请为LinearLayout定位。如果您不指定任何方向,则默认情况下它将以“水平”为准。

这就是为什么您看不到ImageButtons的原因。

<?xml version="1.0" encoding="utf-8"?>

<ScrollView 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"
tools:context=".Activities.HomeFragment">

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

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="216dp"
        android:background="@drawable/lunchimg" />

    <ImageButton
        android:id="@+id/lunch"
        android:layout_width="wrap_content"
        android:layout_height="216dp"
        android:background="@drawable/lunchcate" />

    <ImageButton
        android:id="@+id/dinner"
        android:layout_width="wrap_content"
        android:layout_height="216dp"
        android:background="@drawable/healthyfood" />
</LinearLayout>


 </ScrollView>

希望这会起作用

答案 1 :(得分:0)

您需要使用recyclerview而不滚动。当您希望屏幕适合其他设备时使用滚动。

答案 2 :(得分:0)

此代码是您的回复:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="150dp">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button One" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Two" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Three" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Four" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Five" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Six" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Seven" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Eight" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button Nine" />
    </LinearLayout>
</HorizontalScrollView>