【问题标题】:Android Studio: Layout hierarchy problemsAndroid Studio:布局层次结构问题
【发布时间】:2017-03-25 22:08:50
【问题描述】:

我有一个包含 1 个用作背景的图像的 Horizo​​ntalScrollView。此图像可以从左到右滚动。我想将其他图像视图放在背景图像上,但不让它们随之滚动。目前,如果我在上面放置另一个图像视图,当我滚动图像时也会出现。

最左边的滚动视图:

最右边的滚动视图:

XML 代码:

<?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/activity_rear_gunner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:focusable="true"
tools:context="com.example.mr_br.ibcc_bomber_command.rear_gunner">


<HorizontalScrollView
    android:id="@+id/horizontalScrollView1"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:layout_alignParentTop="true"
>


<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" app:srcCompat="@drawable/background_scroll_2"
    android:id="@+id/scrollView" android:layout_centerVertical="true"
    android:scaleType="centerCrop"

/>

</HorizontalScrollView>
<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/item_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:focusable="true"
    tools:context="com.example.mr_br.ibcc_bomber_command.rear_gunner">

</RelativeLayout>
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" app:srcCompat="@drawable/placeholder_bomber"
    android:id="@+id/enemy" android:layout_centerVertical="true"


/>


</RelativeLayout>

在 android 中使用布局非常新。希望我把我的问题说清楚,如果不是,我会道歉,如果被问到,我会尽力澄清。我知道这可能与我正在使用的布局以及它们的子布局和父布局有关,但就像我说的新的一样。

【问题讨论】:

  • 抱歉,无法解决您的问题。如果敌人是飞机,它不会根据您的图像随着背景滚动。预期的结果是什么?
  • 飞机是敌人是的。我想要的结果是,当我说向右滚动时,飞机保持在左侧的起始位置并离开屏幕,如果我说向左滚动,它又回到屏幕上。
  • 也许创建一个大于屏幕尺寸的视图可以解决这个问题?

标签: java android xml image android-layout


【解决方案1】:

Horizo​​ntalSCrollView 只能有一个子级,但该子级可以是另一个布局,因此您可以添加任意数量的 ImageView。

  <HorizontalScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        >
      <ImageView
          android:layout_width="3000dp"
          android:layout_height="match_parent"
          android:background="#a30"
          />
      <ImageView
          android:layout_width="30dp"
          android:layout_height="30dp"
          android:background="#aa0"
          />
    </RelativeLayout>
  </HorizontalScrollView>

【讨论】:

  • 这对您很有帮助,谢谢。我相信这可以在一定程度上解决我的问题,但我也相信这也是我思考问题的逻辑。但是非常感谢您的帮助,这很有帮助
猜你喜欢
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 1970-01-01
相关资源
最近更新 更多