2012-08-02 169 views
0

對於大屏幕,我有兩個片段,它們應該是頂部/底部或左/右。問題是一個片段正在佔用(幾乎)所有空間佔用一個片段。對於我的活動,我有兩個main.xml(一個用於橫向的肖像一個)。但他們基本上是相似的(除了方向)。Android:片段佈局問題

main.xml中

<?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="wrap_content" 
    android:orientation="vertical" android:id="@+id/frag_cont" > 
</LinearLayout> 

在我的onCreate我片段添加到佈局:

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    OverviewFragment of = new OverviewFragment(); 
    FragmentTransaction tof = getFragmentManager().beginTransaction(); 
    tof.add(R.id.frag_cont, of); 
    DetailFragmentInitial df = new DetailFragmentInitial(); 
    tof.add(R.id.frag_cont, df); 
    tof.commit(); 

} 

兩個片段)onCreateView方面(

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    return inflater.inflate(R.layout.overview, container, false); 

} 
看那種類似

最後,這些片段的兩個xml文件看起來像這樣,首先是較小的片段:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:layout_weight="1"> 


<ImageView 
    android:id="@+id/imageLogo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/logo" 
    android:layout_centerInParent="true" /> 

    <TextView 
    android:id="@+id/textB" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="text" 
    android:layout_above="@id/imageLogo" 
    android:layout_centerHorizontal="true"/> 
</RelativeLayout> 

現在貪婪的一個考慮空間:

<?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="vertical" 
android:layout_weight="10000" > 

<ListView 
    android:id="@+id/ListView01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

</LinearLayout> 

我認爲,當我既layout_weight設置爲1,那麼兩者都將採取相同的空間量,但即使設定1000: 1較大的一個需要約2/3的空間。離開重量會給整個屏幕上的更大的碎片,而另一個不可見。有任何想法嗎?

編輯:

我跟着下面給出的方法。就空間而言,現在好多了。在屏幕上旋轉概覽片段(基本列表視圖)是空的,另一個片段仍然可以。

在OverviewFragment.java我重新填充在onActivityCreated()的片段:

public void onSaveInstanceState(Bundle bundle) 
{ 
    bundle.putSerializable("list", mList); 
    super.onSaveInstanceState(bundle); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) 
{ 
    super.onActivityCreated(savedInstanceState); 

    if(savedInstanceState != null) 
    { 
     mList = (ArrayList<myObject>) savedInstanceState.getSerializable("list"); 
    } 
    else 
    { 
     mAppList = new ArrayList<myObject>(); 
     new getApps().execute(); 
    } 

    ListView lv = (ListView) getActivity().findViewById(R.id.ListView01); 
    lv.setOnItemClickListener(new TableItemSelected()); 
    mItemAdapter = new ItemAdapterOverview(getActivity().getApplicationContext(), mList); 
    lv.setAdapter(mItemAdapter); 
} 
旋轉屏幕的onSaveInstanceState的()被調用,然後再onActivityCreated() 兩次

。第一次調用saveInstanceState不爲null,第二次調用。無論如何,在這兩種情況下,我都應該看到我的清單,無論是從頭開始還是保存清單。爲什麼不是這個方法,爲什麼這個方法被調用兩次?

回答

1

不能完全確定這是否會引發您的問題(我懷疑它),但使用FragmentTransaction.add(..)添加Fragment放入容器內,它實質上取代了與該片斷的容器。這裏的問題是你將兩個碎片添加到同一個容器,所以他們會佔用相同的空間並導致問題。

一個更好的方式來做到這將是兩個集裝箱的意見添加到您frag_cont LinearLayout中(例如FrameLayout S),並給他們兩個單獨的ID(例如frag_onefrag_two)。然後在您的onCreate(..),將其更改爲:

FragmentTransaction tof = getFragmentManager().beginTransaction(); 
tof.add(R.id.frag_one, of); 
DetailFragmentInitial df = new DetailFragmentInitial(); 
tof.add(R.id.frag_two, df); 
tof.commit(); 

這將讓這個ID爲frag_one佈局由OverviewFragment和一個id爲frag_two由DetailFragmentInitial代替填寫。然後,您可以使用main.xml中的視圖來調整片段的大小,只要您認爲合適。

+0

非常感謝,現在旋轉不能正常工作......任何想法?見上面的編輯。 – AndyAndroid 2012-08-02 15:12:35

+0

只是想通了,不要使用tof.add,而是使用tof.replace。 – AndyAndroid 2012-08-02 15:16:18

+0

是的,使用替換似乎比添加工作更加一致。 – 2012-08-02 16:57:08