2016-01-09 50 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,但不适合我 –

相关问题