2016-01-09 77 views
1

如何通過動態完成以下代碼。如何在動態中包含佈局

<include layout="@layout/content_android_dashboard_design" /> 

我需要在我的Slider菜單項中點擊用戶時包含不同的佈局;它在下面給出。

 @SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_home) { 

    } else if (id == R.id.nav_menu) { 

    } else if (id == R.id.nav_hospital) { 

    } else if (id == R.id.nav_atm) { 

    } else if (id == R.id.nav_feedback) { 

    } else if (id == R.id.nav_about) { 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 
+0

看到'#的ViewGroup方法addView'是 – pskink

+0

他們的任何鏈接全球化志願服務青年做 –

+0

你有SDK d ocumentation安裝? – pskink

回答

1

你應該考慮尋找到片段替換如果你希望你的新觀點,以取代舊的。

1聲明您想要片段出現的FrameLayout。

2使每個片段的片段類或重用一個,如果他們看起來相似

3使用

getSupportFragmentManager() 
    .beginTransaction() 
    .replace(R.id.<yourFrameLayoutID>, <anInstaceOfYourFragmentObject>) 
    .commit() 

更換片段也可以將其添加到後面堆棧,使您的後退按鈕功能的工作原理是利用

.addToBackStack(null)/*before*/.commit() 
0
@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 

    View childLayout = null; 

// Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_home) { 
     childLayout = inflater.inflate(R.layout.layout1, (ViewGroup) findViewById(R.id.where_to_include)); 

    } else if (id == R.id.nav_menu) { 
     childLayout = inflater.inflate(R.layout.layout2, (ViewGroup) findViewById(R.id.where_to_include)); 

    } else if (id == R.id.nav_hospital) { 
     childLayout = inflater.inflate(R.layout.layout3, (ViewGroup) findViewById(R.id.where_to_include)); 

    } else if (id == R.id.nav_atm) { 
     childLayout = inflater.inflate(R.layout.layout4, (ViewGroup) findViewById(R.id.where_to_include)); 

    } else if (id == R.id.nav_feedback) { 
     childLayout = inflater.inflate(R.layout.layout5, (ViewGroup) findViewById(R.id.where_to_include)); 

    } else if (id == R.id.nav_about) { 
     childLayout = inflater.inflate(R.layout.layout6, (ViewGroup) findViewById(R.id.where_to_include)); 

    // then add childLayout to your row 
} 



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 
+0

好的。如何添加childLayout到我的行 –

+0

我是traid,但不適合我 –

相關問題