2015-10-14 50 views
1

我把我的Android屏幕/片段分成2部分,如上半部分和下半部分。我通過在父母LinearLayout內放置兩個LinearLayouts來完成此操作。 在上半部分,我保留了一些文字瀏覽和按鈕,不想更改此部分。 在底部的一半,有一個button B。點擊這個Button B,我想導航到一些不同的頁面(或視圖),使上半部分保持不變,只有底部部分發生變化。拆分Android屏幕和做導航只在第二部分

Intent intent=new Intent(getApplicationContext(),DifferentActivity.class); 
startActivity(intent); 

我可以通過創建一個DifferentActivity類(如上所述),其中一次我得把它分成兩個部分,並複製上半部分的粘貼代碼,使在底部的新變化做到這一點半部分。 但假設在主活動底部半部分,有許多按鈕,每個按鈕導航到不同的視圖,然後對於每個視圖,我將不得不創建一個由2個部分組成的活動,包括固定的上半部分。

是否有任何其他方式使導航(onClick按鈕B)僅在底部發生變化,同時保持頂部固定

我的目標是通過點擊底部的Button B來改變底部部分的外觀/界面,盡你所能地去做不同的活動或在同一個父活動中進行更改,做任何事情。

+0

顯示一些代碼(我們航行到新的片段)。 – kebs

回答

1

將兩個LinearLayouts更改爲2個獨立的片段。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <fragment android:name="com.example.android.fragments.FragmentA" 
       android:id="@+id/fragment_a" 
       android:layout_weight="wrap_content" 
       android:layout_height="wrap_content" /> 

    <fragment android:name="com.example.android.fragments.FragmentB" 
       android:id="@+id/fragment_b" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

    </LinearLayout> 

現在你FragmentB內添加onClickListener您按鈕B執行片段交易改變FragmentB到FragmentC

buttonB.setOnClickListener(new OnClickListener() 
{ 

    @Override 
    public void onClick(View v) 
    { 
     final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
     ft.replace(R.id.fragment_b, new FragmentC(), "NewFragmentTag"); 
     ft.commit(); 
    } 

}); 
+0

好主意。 要做一個完整的例子,只需在Android Studio中創建一個導航抽屜項目。 您可能只需將其從側面切換到頂部樣式即可。 –