2016-04-14 33 views
7

每当我在设计器表面添加新的容器视图并将layout:widthlayout:height设置为wrap_content时,容器视图大小超出了它应该如下所示的大小。在Android Studio预览版中,容器伸展超越

我该怎么做才能解决这个问题?的content_main.xml

enter image description here

编辑

Android Studio

代码片段:可能是从上图中,但仍是同样的问题,有点不同。

<?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可以计算它的高度和宽度之前,需要使用列表视图项来填充listview。因此,如果没有测量的高度和宽度,则无法按照您的要求包装内容。我不认为有一种好的方法可以解决这个问题,而不是用xml中的示例内容填充listview。 –

+0

发布您的XML代码片段,而不是在图像中提供。 –

回答

2

使用

的android:layout_width = “match_parent” 机器人:layout_height = “match_parent”

+0

不幸的是,将'android:layout_width/height'同时更改为'match_parent'并未改变预览行为。 – Sung

+0

如果这是类似html的东西,如果你匹配父类,但是将padding-top和padding-bottom设置为0dp会发生什么? –

+0

@PaulTotzke仍是同样的问题。我正在学习一个教程。在教程中,当作者将所有填充设置为0时,列表视图占据整个屏幕。但在我的情况下,它不会发生。 – Sung

1

胡乱猜测,但尝试使用,而不是一个RelativeLayout的滚动型。

或在布局内,所以它将保持相同的大小时,列表视图填充和不拉伸。

1

这是因为你的 app:layout_behavior="" 试图改变它或删除它,你的容器大小将不会超越。

+0

设置'app:layout_behavior =“”'向上移动了上方和后面的操作栏列表视图。这是它现在的样子。 http://i.imgur.com/LE8JpEy.png – Sung

1

尝试改变warp_contentmatch_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" /> 
2

没有什么不妥。

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