【问题标题】:Containers stretch beyond in Android Studio preview容器超越了 Android Studio 预览版
【发布时间】:2016-04-14 00:36:03
【问题描述】:

每当我在设计器图面上添加一个新的容器视图并将layout:widthlayout:height 设置为wrap_content 时,容器视图的大小都会超出其应有的大小,如下所示。

我应该怎么做才能解决这个问题?

content_main.xml的代码sn-p

编辑:可能与上图略有不同,但问题仍然相同。

<?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: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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.dance2die.myapplication.MainActivity"
    tools:showIn="@layout/activity_main">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:padding="0dp" />
</RelativeLayout>

【问题讨论】:

  • 我相信会发生这种情况是因为列表视图需要先填充列表视图项,然后列表视图才能计算其高度和宽度。因此,如果没有测量的高度和宽度,它就无法按照您的意愿包装内容。除了用 xml 中的示例内容填充列表视图之外,我认为没有其他好的方法可以解决此问题。
  • 发布您的 XML 代码 sn-p 而不是在图像中提供。

标签: android-layout android-studio


【解决方案1】:

使用

android:layout_width="match_parent" android:layout_height="match_parent"

【讨论】:

  • 不幸的是,将android:layout_width/height 更改为match_parent 并没有改变预览行为。
  • 如果这类似于 html,如果匹配 parent 但将 padding-top 和 padding-bottom 设置为 0dp 会发生什么?
  • @PaulTotzke 还是同样的问题。我正在关注一个教程。在教程中,当作者将所有的 padding 设置为 0 时,listview 占据了整个屏幕。但就我而言,它不会发生。
【解决方案2】:

这并没有错。

预览只是向您显示使用Simple 2-Line List item 的预览。它被拉伸是因为您将其设置为wrap content,因此如果其中有超过 8 个项目,ListView 实际上会比您的屏幕长(至少在该设备上的预览中,可能在 Nexus 的预览中) 6 你会看到 10 个项目)。

【讨论】:

    【解决方案3】:

    一个疯狂的猜测,但尝试使用滚动视图而不是相对布局。

    或者在布局中,这样当列表视图被填充并且不拉伸时它会保持相同的大小。

    【讨论】:

      【解决方案4】:

      这是因为你的 app:layout_behavior="" 尝试更改或删除它,您的容器大小不会超出。

      【讨论】:

      • 设置app:layout_behavior="" 将列表视图向上移动到操作栏的上方和后方。这是它现在的样子。 i.imgur.com/LE8JpEy.png
      【解决方案5】:

      尝试将warp_content更改为match_parent

      <ListView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/listView"
          android:layout_alignParentTop="true"
          android:layout_alignParentStart="true"
          android:padding="0dp" />
      

      【讨论】:

        【解决方案6】:

        您的顶部菜单栏正在将您的子 div 推低,或者您的软导航键正在“浮动”。

        首先,尝试隐藏状态栏(顶部)。 http://developer.android.com/training/system-ui/status.html

        其次,在 Android 虚拟设备管理器中尝试一个没有虚拟按钮栏的新设备,例如 Galaxy s7 或尝试隐藏它。 http://developer.android.com/training/system-ui/navigation.html

        【讨论】:

          猜你喜欢
          • 2015-08-12
          • 1970-01-01
          • 1970-01-01
          • 2016-06-14
          • 1970-01-01
          • 1970-01-01
          • 2020-12-05
          • 2015-08-22
          • 1970-01-01
          相关资源
          最近更新 更多