<activity android:windowSoftInputMode=["stateUnspecified",
</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>