在其他布局的LinearLayout中扩展布局

我有这个布局:

ComposeView http://img845.imageshack.us/img845/2121/d6zp.png

2个边框(左,右)由图标填充。 当我触摸其中一个图标时,我可以访问其他活动。 顶部黑条是自定义标题栏。

清晰的灰色内部空间是我需要适应我在我的应用程序上所有活动的地方。 所以这种布局就像菜单布局,在所有活动中都是静态的。

这是布局xml:

menu_view.xml

       ...    ...     

这是定义所有图标onClickListeners的类。

MenuView.java

 public class MenuView extends RelativeLayout { private final LayoutInflater inflater; Context context; public MenuViewActivity(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.menu_view, this, true); ((ImageView)this.findViewById(R.id.navButton)).setOnClickListener(launch_nav); } final OnClickListener launch_nav = new OnClickListener() { @Override public void onClick(View v) { getContext().startActivity(new Intent(getContext(), Navigation.class)); } }; 

好吧,有了这个(我不确定它是否一切正常,也许我对inflate方法或类似的东西做错了),现在的问题是将其他activitie的布局定义在这个视图中。 为此,我写道:

ExampleActivity.java

 public class ExampleActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout inside_menu_view = (LinearLayout)findViewById(R.id.activitycontent); View this_layout = getLayoutInflater().inflate(R.layout.main, inside_menu_view, true); inside_menu_view.addView(this_layout); 

但是我在最后一行得到了一个N​​ullPointerException。 因此,当在这个片段上膨胀时,一定是错误的。

嘿使用以下结构。 放。 在每个活动布局中包含您的通用布局

            

您可以使用FragmentActivity扩展的一个主要活动,如BaseActivity。 基本活动扩展了SlidingFragmentActivity并实现了菜单等基本结构.FragmentActivity onCreate方法是:

@覆盖

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); fm = getSupportFragmentManager(); //This is main content that is switchable mContent = new MainListActivity(); //Additionally you can define those two lines as fragments and add them as default on both sides : sideFragments = new sideFragments(this); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.content_frame, mContent); ft.replace(R.id.side_framgments, sideFragments); ft.commit(); } 

当您从菜单中按下其中一些按钮(左右边框)时,您将使用FragmentActivity中的此function更改屏幕中间的片段:

  public void switchContent(Fragment fragment, String tag) { mContent = fragment; getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,fragment).addToBackStack(tag).commit(); } 

注意R.id.content_frame是可以在这两行之间切换的XML(比如两边的菜单),而R.id.side_framgments是可切换的主布局之间的那两行。