對於縱向和橫向模式,我有不同的佈局。 layout\activity_main.xml
:當針對不同的方向使用不同的佈局時,在方向更改時保存片段狀態
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout android:id="@+id/container"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>
而且layout-large\activity_main.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentorientationchangetest.Fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragmentorientationchangetest.Fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
我的片段是相同的,和fragment2.xml
:
<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"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2" />
<EditText android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_below="@+id/text"/>
</RelativeLayout>
我要的是,當我在編輯文本在我的片段輸入一些文字並且我旋轉屏幕,編輯文本不會丟失其輸入文本。
由於縱向和橫向模式下的片段不是一個實例,因此我不能使用setRetaintInstance
或onSaveInstanceState
。我怎樣才能做到這一點,而不需要定義兩個活動,並且不使用onConfigurationChanged
?
注意:不添加任何代碼,每個片段保存其數據。所以如果我在肖像模式下在fragment2中輸入一些文字,然後再旋轉到橫向並再次旋轉到肖像模式,我會看到輸入的文字。
「我想要的是,當我在片段中的編輯文本中輸入一些文本時,旋轉屏幕,編輯文本不會丟失其輸入文本。」和「注意:不添加任何代碼,每個片段都會保存其數據,因此,如果我在肖像模式下輸入片段2中的某些文本,並再次旋轉到橫向並再次旋轉到肖像模式,則會看到輸入的文本。 **所以有什麼問題**和**方面注意:如果你要編輯你的問題添加編輯或更新後一些人回答** – mmlooloo 2014-10-03 06:49:50
@ mmlooloo我在肖像模式。我在編輯文本中輸入一些文本,然後旋轉到橫向。我沒有看到輸入的文本,然後再次將屏幕旋轉到縱向模式,並看到輸入的文本。我應該怎麼做才能看到從陸地到港口和反過來的文字? – 2014-10-03 07:16:34
因爲你的片段不是一個實例,你的問題實際上屬於片段通信類別,我認爲我的解決方案是最簡單的。你可以等待其他人,但我的解決方案就是我的答案。 – mmlooloo 2014-10-03 07:26:39