这个demo演示了当系统的软键盘打开时,window布局的调整方式 可以通过设定WindowManager.LayoutParams参数来实现不同的调整模式
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/hello_world"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:paddingTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Resize mode:"/>
<Spinner
android:id="@+id/resizeMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="This is the part of application's UI that can resize to adjust for the IME"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#ff0000" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#00ff00"
android:text="Text editor.Tap to show the IME,which will cause to this window to resize as requested"/>
</LinearLayout>
MainActivity
public class MainActivity extends Activity {
private Spinner mResizeMode;
private CharSequence[] mModeLabels = new CharSequence[] { "Unspecified",
"Resize", "Pan", "Nothing" };
private int[] mResizeModes = new int[] {
// 未指定键盘的显示模式,系统根据内容自行从resize和pan两种模式中选择一种
// 或是继续使用上次的显示模式
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED,
// 整个Layout重新编排,重新分配多余空间
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE,
// 把整个Layout顶上去露出获得焦点的EditText,确保输入焦点可见
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN,
// 不调整(输入法完全直接覆盖住)
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mResizeMode = (Spinner) findViewById(R.id.resizeMode);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
this, android.R.layout.simple_spinner_item, mModeLabels);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mResizeMode.setAdapter(adapter);
mResizeMode.setSelection(0);
mResizeMode.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
getWindow().setSoftInputMode(mResizeModes[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
getWindow().setSoftInputMode(mResizeModes[0]);
}
});
}
}