解决EditText不能撑满全屏的问题及EditText你应该知道的属性

一般我们要实现去下图一的效果很简单:
两个EditText就搞定
效果图一:
 
但是我们想让第二个EditText撑满剩余空间怎么做?如效果图二
效果图二:
解决:

使用了ScrollView嵌套LinearLayout,将ScrollView中android:fillViewport设置为true。

分析:

当ScrollView里的元素想填满ScrollView时,使用"fill_parent"是不管用的,必需为ScrollView设置:android:fillViewport="true"。

当ScrollView没有fillVeewport=“true”时, 里面的元素(比如LinearLayout)会按照wrap_content来计算(不论它是否设了"fill_parent"),而如果 LinearLayout的元素设置了fill_parent,那么也是不管用的,因为LinearLayout依赖里面的元素,而里面的元素又依赖 LinearLayout,这样自相矛盾.所以里面元素设置了fill_parent,也会当做wrap_content来计算.


 

代码如下,注释了常用的EditText属性:
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true">    //*******************关键处********
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <EditText
                android:id="@+id/edit_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center|left"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:background="@null"    //控件背景,这里没有,指透明
                android:ellipsize="end"    //自动隐藏尾部溢出数据,一般用于文字内容过长一行无法全部显示时
                android:hint="添加标题"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                android:singleLine="true"    //强制输入的内容在单行
                android:textColorHint="#bfbfbf" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/dotted_line"
                android:layerType="software" />
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <EditText
                    android:id="@+id/edit_content"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:layout_marginTop="16dp"
                    android:gravity="left|top"     //输入时光标在左上角
                    android:hint="内容"
                    android:lineSpacingExtra="4.6dp"    //设置行间距
                    android:scrollbars="vertical"    // 设置滚动条显示
                    android:textColorHint="#bfbfbf"
                    android:textSize="16sp" />
            </RelativeLayout>
            <!--
                    android:background="@null"    //去掉EditView的边框
                    android:inputType="textMultiLine"    //可以显示多行
                    android:minLines="6"    // 设置文本的最小行数
            -->
        </LinearLayout>
    </ScrollView>
posted @ 2016-03-24 11:54  吹泡泡`OOO  阅读(749)  评论(0编辑  收藏  举报