android - 强制编辑文本在 ScrollView 中占据全屏

标签 android android-layout

我在 ScrollView 中的 LinearLayout 中有一个 EditText 字段。只要 ScrollView 不存在,layout_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>

我似乎不明白我做错了什么。请就如何让 EditText 从 ScrollView 中的 LinearLayout 中占据屏幕的其余部分提出任何建议。

谢谢。

编辑 再次面临同样的问题,这是我的布局。

<?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>

最佳答案

需要添加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" >
....

关于android - 强制编辑文本在 ScrollView 中占据全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7047626/

相关文章:

android - Gradle aapt.exe 错误

android - 重复调用 LineBackgroundSpan drawBackground()

android - 如何在点击编辑文本时播放声音

android - Android 8 上通知 channel 的默认优先级

java - 数据库文件不创建 Android

android - 如何在android中实现双指缩放

android - 仅在华为设备中关闭应用程序时服务被杀死

android - 为什么 margin 在 Android 中覆盖 marginLeft(与 radius 等相同)?

android - 如何在android中的相机预览中绘制矩形

android - 使用 xml 布局使用多个 TextView 创建段落