【问题标题】:ScrollView is not working properly with the ImageButtonsScrollView 无法与 ImageButtons 一起正常工作
【发布时间】:2020-03-04 18:43:45
【问题描述】:

我想在我的主页中有几个 ImageButtons,所以它应该在一个 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>

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

【问题讨论】:

  • 尝试在线性布局中添加 android:orientation="vertical"。

标签: android scrollview


【解决方案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>

希望这会奏效

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      此代码是您的响应:

      <?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>
      

      【讨论】:

        猜你喜欢
        • 2020-08-19
        • 1970-01-01
        • 2013-11-24
        • 1970-01-01
        • 2017-12-14
        • 2013-02-06
        • 2019-02-02
        • 2021-10-15
        • 2016-07-05
        相关资源
        最近更新 更多