这个LinearLayout应该使用android:layout_height="wrap_content"吗?

9
我有以下的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" // ==> here I get the error.
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"  />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:background="#298EB5"
        android:orientation="horizontal" />


  </LinearLayout>
</ScrollView>

但我收到了lint消息:

这个LinearLayout应该使用android:layout_height="wrap_content"

为什么会出现这个消息?

3个回答

10
LinearLayout 是用于将元素堆叠在一起,可以横向排列或纵向排列。我猜测这个 lint 警告建议使用纵向排列,因为有 ScrollView文档

"LinearLayout 的所有子元素都会依次堆叠在一起,所以一个竖直列表每行只能有一个子元素,无论它们有多宽;而一个水平列表只有一行(高度等于最高子元素的高度加上填充)。 LinearLayout 会尊重子元素之间的边距和每个子元素的对齐方式(右、中、左对齐)。


4

它是Lint警告,您应该使用

android:layout_height="wrap_content"

wrap_content 将占用所需内容的高度。这意味着,布局的高度将根据所需内容而定。


4

这不是一个错误,但在某些情况下会产生不必要的结果,因此不建议使用。在使用scrollview时,我遵循了Romain的文章。希望这可以解释这条消息出现的原因。


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