强制使一个编辑文本在滚动视图中占据整个屏幕

5

我有一个EditText字段,它在一个LinearLayout中,并且该LinearLayout又在一个滚动视图中。只要没有ScrollViewlayout_weight="1.0"可以让我的EditText占据剩余的屏幕空间。但是一旦我将LinearLayout包裹在ScrollView中,它就会将EditText更改为单行,并且layout_weight不起作用。

我的完整布局文件:

<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent" android:layout_height="fill_parent"     android:orientation="vertical" >        

<TextView android:id="@+id/titleTextView"       
          style="@style/TitleHeaderBarText"     
          android:text="@string/announcement" />        

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout  
         android:layout_width="match_parent" 
         android:layout_height="fill_parent"    
         android:orientation="vertical" 
         android:gravity="right" >

                <TextView 
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Subject:" 
                    android:textSize="16sp" />

                <EditText 
                    android:id="@+id/et_subject"
                    android:layout_width="match_parent" 
                    android:layout_height="wrap_content" />

               <TextView
                    android:layout_width="match_parent" 
                    android:layout_height="wrap_content"
                    android:text="Announcement:" 
                    android:textSize="16sp" />

                <EditText
                    android:id="@+id/et_announcement"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1.0" />

                <Button
                    android:id="@+id/btn_save"
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content"
                    android:paddingLeft="24sp" 
                    android:paddingRight="24dp"
                    android:text="Save" />      

       </LinearLayout>  
</ScrollView>

</LinearLayout>

我似乎不明白我做错了什么。请问如何在ScrollView中的LinearLayout内使EditText占据剩余屏幕空间?请提供建议。
谢谢。 编辑 再次遇到同样的问题,这是我的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/titleTextView"
        style="@style/TitleHeaderBarText"
        android:text="@string/login_title" />

    <ScrollView
        android:id="@+id/sv_weight_tell_us"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:background="@drawable/bg_light_radial_pink" >
        <LinearLayout 
            android:layout_width="match_parent" android:layout_height="match_parent"
            android:padding="12dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/login_header"
                android:layout_width="match_parent" android:layout_height="wrap_content"
                android:text="Login" />

            <TableLayout 
                android:layout_width="match_parent" android:layout_height="wrap_content"
                android:stretchColumns="1" >
                <TableRow>
                    <TextView
                        android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:paddingRight="20dp"
                        android:text="@string/username" />
                    <EditText
                        android:id="@+id/et_username"
                        android:layout_width="wrap_content" android:layout_height="wrap_content" />
                </TableRow>
                <TableRow>
                    <TextView
                        android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:text="@string/password" />
                    <EditText
                        android:id="@+id/et_password"
                        android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:inputType="textPassword" />
                </TableRow>
                <TableRow>
                    <View/>
                    <Button
                        android:id="@+id/btn_login"
                        android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:text="@string/login"
                        android:padding="12dp" />
                </TableRow>
            </TableLayout>

            <ProgressBar
                android:id="@+id/pb_1"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:indeterminate="true"
                style="?android:attr/progressBarStyleSmall"
                android:visibility="invisible" />

            <TextView
                android:layout_width="wrap_content" android:layout_height="match_parent"
                android:layout_weight="1.0" android:gravity="bottom"
                android:text="@string/dont_have_Q" />           
            <TextView
                android:id="@+id/tv_sign_up"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="@string/sign_up_" />

        </LinearLayout>
    </ScrollView>

</LinearLayout>

EditText需要具有layout_weight属性吗?如果您的LinearLayout的宽度设置为match_parent,则将EditText设置为match_parent应该填充屏幕,因为其方向是“垂直”。 - hooked82
谢谢回复。事实上,这也是我在发帖之前想到并尝试过的方法,但并没有起作用。 - achie
重新查看您的布局文件,我发现缺少了开始的<ScrollView>标签。您的实际布局XML中也是这样吗? - hooked82
不知为何,这里的语法格式化程序将其删除了,但当我去编辑它时,它还在那里。但无论如何,当我在“<”和“ScrollView”之间加一个空格时,它似乎会出现。希望您现在能看到它。 - achie
1个回答

18

你需要添加ScrollView属性android:fillViewport="true"

...
<ScrollView
    android:id="@+id/sv_weight_tell_us"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@drawable/bg_light_radial_pink"
    android:fillViewport="true" >
....

确实可以工作。谢谢。不知道fillViewport标签是用来做什么的。 - achie

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