Android软键盘弹出时布局的调整问题

    我们可以通过下面这条属性来设置软键盘的初始状态和对布局的影响   具体含义查看文档

<activity android:windowSoftInputMode=["stateUnspecified",

                         "stateUnchanged", "stateHidden",

                         "stateAlwaysHidden", "stateVisible",

                         "stateAlwaysVisible","adjustUnspecified",

                         "adjustResize", " …… >

</activity>

1。当不希望软键盘挤压布局时(直接覆盖下半部分)   设置该属性为android:windowSoftInputMode="adjustNothing"  即可


2.当Edittext处于屏幕下方时,上述办法就不好用了,这时候不去设置该属性,软键盘的出现将会挤压我们的布局,如果存在含有可滚动的视图,窗口将会重新调整,给软键盘留出空间,并且软键盘自动弹出,代码如下所示,“填充字段1”和”填充字段2“将被压缩,如果不存在,我们的布局的上半部分将会移出屏幕,如果需要来回拉动布局,只要在最外层加垂直的ScrollView即可。

<pre name="code" class="html"><LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
     >
   <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="填充字段1"
        android:scrollbars="vertical" //<span style="background-color: rgb(255, 0, 0);">重点,加上这个滚动条,软键盘将自动弹出,并挤压该视图来为软键盘提供空间</span>
	/> 
	<TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="填充字段2"
	/> 
	 <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="用户名"
            />
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="密码"
            />
</LinearLayout>

 

3.如果不希望软键盘遮挡下半部分,同时不希望上半部分移出屏幕,可以设置android:windowSoftInputMode="adjustResize"

同时,将可以压缩的部分高度设为0dp,并使用android:layout_weight属性,代码如下:


 

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"    
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
     >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="顶部视图"
	/> 
   <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="压缩字段1"             //软键盘出现时,该部分压缩
	/> 
	<TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="压缩字段2"            //软键盘出现时,该部分压缩
	<EditText 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content"</span>
	android:hint="用户名" /> </span>
	<EditText 
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:hint="密码" />
</LinearLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值