容器在Android Studio预览中超出范围

7
每当我在设计师界面上添加一个新的容器视图,并将layout:widthlayout:height都设置为wrap_content时,容器视图的大小超出了应有的范围,如下所示。
我该怎么解决这个问题? < p >Android Studio

< p > < code >content_main.xml的代码片段

enter image description here

编辑:可能与上面的图像略有不同,但仍然存在相同的问题。
<?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>

2
我认为这是因为在listview计算其高度和宽度之前,需要使用list view项填充listview。因此,在没有测量高度和宽度的情况下,它无法像您想要的那样包装内容。我认为除了在xml中使用示例内容填充listview之外,没有更好的解决方法。 - Da-Jin
请发布您的XML代码片段,而不是提供图片。 - Jay Rathod
6个回答

2

没有任何问题。

预览仅显示使用简单的2行列表项的预览。它被拉伸,因为您将其设置为wrap content,所以如果其中有超过8个项目,则ListView实际上会比您的屏幕更长(至少在该设备的预览中,可能在Nexus 6的预览中,您会看到10个项目)。


2

使用

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

这是设置Android布局宽度和高度的代码,其中“match_parent”表示该视图应该与其父视图的大小匹配。


不幸的是,将android:layout_width/height都更改为match_parent并没有改变预览行为。 - dance2die
如果这与HTML类似,那么如果您匹配父元素但将padding-top和padding-bottom设置为0dp会发生什么? - Paul Totzke
@PaulTotzke 问题仍然存在。我在跟着教程做,教程中作者将所有padding设置为0时,listview占据了整个屏幕。但在我的情况下并没有发生这种情况。 - dance2die

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

设置 app:layout_behavior="" 将 ListView 上移并隐藏到 ActionBar 后面。以下是现在的样子:http://i.imgur.com/LE8JpEy.png - dance2die

1
尝试使用ScrollView而不是RelativeLayout,这只是一个猜测。
或者在布局内部使用它,这样当ListView被填充时,它的大小将保持不变,不会被拉伸。

1
尝试将 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" />

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接