<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">要使用adjustResize的属性必须要先布局好,当然前提是这个activity不能是FullScreen的,否则设置的adjustResize将失效,这句是看网上人说的,具体没有去测试。</span>
目前项目中的不是FullScreen,但是由于是别人写的布局代码,然后发现在配置文件里配置了adjustResize属性,但是这个View就是不能随着键盘的显示而变换size,
于是写了个测试的android项目,首先模拟了这个布局的轮廓,就是写LinearLayout嵌套LinearLayout嵌套widget等的东西,如:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/head"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#555666"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_below="@id/head"
android:background="#ffffff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="150dp"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/edt"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="@string/edit_str" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!--
<EditText android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="@string/edit_str"
android:id="@+id/edt"
android:layout_above="@+id/text"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fasdfasdgfdgfg"
android:id="@+id/text"
android:layout_above="@+id/btn"/>
<Button android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fadsfsafsd"
android:layout_alignParentBottom="true"
/>
-->
</RelativeLayout>
测试这个布局,点击linearlayout里面的edit的时候即使activity配置了adjustResize属性依然不起效。但是如果修改这个布局,将里面的
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_below="@id/head"
android:background="#ffffff"
android:orientation="vertical" >
修改成
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:layout_below="@id/head"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="bottom" >
然后就可以实现adjustResize的属性特性了,为什么会这样呢?我认为可能跟布局的layout的绘画顺序机制有关系,
因为这个Linearlayout的gravity为bottom的时候,是将layout里面的widget放在这个layout的最底下,也就是说,这个layout的top可以是空白的(前提是这个layout比包裹的widget高更大),这时这个adjustResize就起效了,但是如果将layout的gravity改成top的话这个adjustResize就失效了,即使这个layout正好包裹里面的widget,甚至是wrap类型这个adjustResize也是失效的。如:
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/head"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="top" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="150dp"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/edt"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="@string/edit_str" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
所以总结一下如果要使adjustResize起效的话,必须要考虑好布局,布局必须要让最底下的widget或者layout的位置为android:layout_alignParentBottom="true"才能使之生效。
但是有时候如果说edit在中间,但是edit下面的layout的高度太长了,如果这样做的话,使用adjustResize是可以实现它的效果,resize,但是很可能看不到这个edit,这个edit被挤出去了,如果是这种情况我觉得可能需要去监听下最外层的layout的onsizechanged事件,然后在该事件里重新修改它的布局……具体要测试使用了才知道。感兴趣的测试下吧,欢迎把结果告诉我,谢谢!