我製作了兩個佈局文件 - 一個用於縱向,一個用於橫向。這裏用於肖像:Android - 使用分段時處理方向更改
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/fragment_newslist"
android:name="com.app.NewsListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
</fragment>
</LinearLayout>
這裏用於景觀:
<fragment
android:id="@+id/fragment_newslist"
android:name="com.app.NewsListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
</fragment>
<fragment
android:id="@+id/fragment_viewnews"
android:name="com.app.ViewNewsFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" >
</fragment>
然後創建它加載佈局在onCreate()方法一個活動。到目前爲止,這當然很好。此活動不包含比這更多的代碼。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
}
在NewsListFragment類中我檢查ViewNewsFragment是否可用。如果不是,並且用戶點擊一個ListItem,一個新的Activity(即ViewNewsActiviy)將被啓動。如果可用,數據將顯示在現有片段中。所以有兩個類:1. ViewNewsActivity和2. ViewNewsFragment
但我真正想要的是改變佈局方向的變化。當設備從縱向轉爲橫向時,我想擁有典型的雙面板佈局,並且如果從橫向轉爲縱向,我想單獨顯示列表,並將細節視爲單獨的「視圖」。
但如何做到這一點?直到現在,當您以橫向或縱向方式啓動應用程序時,它都能正常工作。但是當你改變方向時,佈局保持原來的狀態。
我真的很感激任何幫助:)! 非常感謝!
延
是的,但我不會重新加載活動。我只想刪除一個片段。 – user1177708 2012-01-31 08:15:21
@ user1177708:請允許Android銷燬並重新創建您的活動。如果您希望在舊活動和新活動之間保留片段,請使用「FragmentTransaction」創建它們,並將片段調用爲「setRetainInstance(true)」。 – CommonsWare 2012-01-31 12:26:33