使用adjustResize屬性筆記

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   使用   

<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事件,然後在該事件裡重新修改它的布局……具體要測試使用了才知道。感興趣的測試下吧,歡迎把結果告訴我,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.