ScrollView中的min height fill_parent和height wrap_content是什么?(或者只是电子邮件应用程序撰写布局的代码)

33

问题 如果我有以下内容:

<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="?" android:orientation="vertical">
        <EditText android:layout_width="fill_parent" android:layout_height="?" android:hint="Subject" android:id="@+id/subject" />
        <EditText android:layout_width="fill_parent" android:layout_height="?" android:id="@+id/body" />
    </LinearLayout>
</ScrollView>

我该如何让正文(第二个EditText)填满剩余的屏幕,但当正文内容太长时仍然启用ScrollView?就像 height="wrap_content"minHeight="fill_parent" 一样。

如果将它们放在ScrollView中,layout_height="fill_parent" 似乎不起作用。

符合我要求的一个工作示例是电子邮件应用程序的撰写窗口

我尝试过这个方法,但EditText元素表现得像是wrap_content,没有填充发生。只有在键入足够多的内容时才会滚动。

<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
        <EditText android:layout_width="fill_parent" android:layout_height="fill_parent" layout_weight="1" android:hint="Subject" android:id="@+id/subject" />
        <EditText android:layout_width="fill_parent" android:layout_height="fill_parent" layout_weight="1" android:id="@+id/body" />
    </LinearLayout>
</ScrollView>
4个回答

91

我在另一个问题的已接受答案中发现了一条链接,指向邮件应用程序源代码。回答很简单!在<ScrollView...> 中添加 android:fillViewport = "true",并在您希望占用所有可用空间的视图上添加android:layout_weight="1.0"。在这个博客文章中有解释。


6
只是为了那些看到这个的人,不要忘记你需要像我上面做的那样指定重量。 - 700 Software
Romain Guy在博客文章中解释了这个技巧:http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/ - caw
1
@MarcoW,你怎么让ScrollView让其子元素占用所需的所有空间,但是又限制ScrollView的最大高度呢?例如:最大高度为100dp。如果子元素需要的高度小于该值,则子元素和ScrollView的高度都将设置为该值(小于100dp)。如果子元素需要100dp或更多,则它们两个的高度都将设置为100dp。 - android developer
博客文章的链接无法使用,我通过网络档案馆阅读了它。 - lelloman

7

如上所述,正确的方法是使用android:fillViewport="true"属性。

这里有一个例子,在这个例子中,图片将被放置在scrollView的末尾:

  <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">
    <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:fillViewport="true"
      android:orientation="vertical">

      <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">
        <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="test"/>
      </LinearLayout>


      <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/red">
        <ImageView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:adjustViewBounds="true"
          android:src="@drawable/about_us_image_1_land"
          style="@style/layout_marginTop20"/>
      </LinearLayout>
    </LinearLayout>
   </ScrollView>

1
你应该使用match_parent而不是fill_parent。赞一个示例! - Nathan

0

调用之后

AlertDialog dialog = builder.show();

然后调用

fixScrollViewHeight(scrollView);

private void fixScrollViewHeight(ScrollView scrollView)
{
    int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
    LayoutParams lp = scrollView.getLayoutParams();
    lp.height = screenHeight / 3;
    scrollView.setLayoutParams(lp);
}

0
为什么不将ScrollView设置为fill_parent,而将A和B设置为wrap_content呢? 你想要的唯一一件事就是让ScrollView适应屏幕。

我也希望文本区域B可以填满屏幕,类似于min-height:fill_parent的效果。 - 700 Software
尝试使用LinearLayout,并在每个A和B上使用layout_weight="1",并且在A和B上也使用layout_height="fill_parent"。 - fedj

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