2010-05-13 72 views
3

在我的Android应用程序中,我有一个选项卡Activity。在其中一个标签中,我有两个TextView和两个EditText s。第一个EditText只有一行,这没关系。但是,我希望其他EditText,android:id="@+id/paste_code"占用剩余的空间,但无论我做什么,它只会显示一行。我不想手动设置行数,因为适合屏幕的数量因您的设备而异。Android EditText不会占用剩余空间

下面是相关的代码。它嵌套在标签为Activity的所有必要组件中。

<ScrollView 
    android:id="@+id/basicTab" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Paste title" 
      android:layout_weight="0" /> 
     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/paste_title_hint" 
      android:id="@+id/paste_title" 
      android:lines="1" 
      android:gravity="top|left" 
      android:layout_weight="0" /> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Paste text" 
      android:layout_weight="0" /> 
     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:hint="@string/paste_hint" 
      android:id="@+id/paste_code" 
      android:gravity="top|left" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</ScrollView> 

回答

9

由于接受的答案不能完全解决的情况下,这里的人们来此同时寻找适当的解决办法:

首先,罗曼盖伊从Android开发团队解决了这个好这个博客帖子:

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

从根本上讲,ScrollView需要包含android:fillViewport="true"属性。

如果事情不工作,一旦你做到了这一点,这里有几件事情要检查:

  • ScrollView(如LinearLayout)需要布局有layout_height="wrap_content"
  • 要扩大期(县)应具有layout_height="wrap_content"
  • 视图()你想扩展应该有layout_weight="1.0"或类似

如果您不想让它缩小太多,请不要忘记在要扩展的视图中设置minLines="3"或类似物。

+0

确实;谢谢你。我用另一个SO问题的链接评论了另一个答案,这个问题解决了我的问题。 – Jamie 2010-10-12 00:46:52

+0

工程就像一个魅力! – Damjan 2011-09-30 10:32:50

0

该问题似乎来自您使用ScrollView。我已经使用ScrollView作为父容器测试了您的代码,并且遇到了同样的问题。但是,如果我用LinearLayout替换了ScrollView,则第二个EditText正确扩展以填充整个屏幕。问题一定是ScrollViews被设计成可以包装成尽可能小的尺寸,而不管你在android:layout_height中放置了什么设置。我尝试了其他几种布局,例如使用layout_abovelayout_below的RelativeLayout,但这些只影响其最大尺寸,而不影响其空白时的尺寸。不幸的是,这意味着我不知道如何解决你的问题......有没有一种方法可以重新设计你的布局使用除ScrollView之外的东西作为父容器?

+0

是的,这个标签可能没有ScrollView,因为(如果我没记错的话)EditTexts有自己的滚动条。我会玩一个LineaLayout。 谢谢。 – Jamie 2010-05-13 23:20:42

+0

此外,我刚刚发现http://stackoverflow.com/questions/2033296/android-scrollview-problem它肯定了你的android语句:layout_height没有做任何事情,并提供另一个XML属性,使其占用屏幕的其余部分。如果这不起作用,我会诉诸LinearLayout。 – Jamie 2010-05-13 23:25:54