【问题标题】:Android EditText won't take up remaining spaceAndroid EditText 不会占用剩余空间
【发布时间】:2010-05-13 22:10:57
【问题描述】:

在我的 Android 应用程序中,我有一个标签式 Activity。在其中一个标签中,我有两个TextViews 和两个EditTexts。

第一个EditText 只有一行,没关系。但是,我希望另一个EditTextandroid:id="@+id/paste_code" 占用剩余空间,但无论我对它做什么,它都只会显示一行。我不想手动设置行数,因为适合屏幕的行数因设备而异。

这是相关代码。它嵌套在标签式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>

【问题讨论】:

    标签: android


    【解决方案1】:

    由于接受的答案并没有完全解决这种情况,这里有一个适当的解决方案,供人们在搜索时找到:

    首先,来自 Android 开发团队的 Romain Guy 在这篇博文中很好地解决了这个问题:

    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" 或类似名称。

    【讨论】:

    • 确实;谢谢你。我评论了另一个答案,并附上了另一个解决了我的问题的 SO 问题的链接。
    【解决方案2】:

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

    【讨论】:

    • 是的,如果没有 ScrollView,这个选项卡可能会很好,因为(如果我没记错的话)EditTexts 有自己的滚动条。我将使用 LineaLayout。谢谢。
    • 另外,我刚刚发现stackoverflow.com/questions/2033296/android-scrollview-problem 确认了您的声明 android:layout_height 没有做任何事情,并提供了另一个 XML 属性以使其占据屏幕的其余部分。如果这不起作用,我将求助于 LinearLayout。