這個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();
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[]);
}
});
}
}