java在其他布局的LinearLayout中为布局充气
我有这样的布局:
ComposeView http://img845.imageshack.us/img845/2121/d6zp.png
两个边框(左、右)由图标填充。当我触摸其中一个图标时,我将进入其他活动。顶部的黑色栏是一个自定义标题栏
我需要将应用程序中的所有活动都放在清晰的灰色内部空间中。所以这个布局类似于菜单布局,在所有活动中都是静态的
这是布局xml:
菜单\u视图。xml
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.MenuView" >
<!-- Show top black custom title bar-->
<include
layout="@layout/custom_tittlebar" >
</include>
<!-- Button columns -->
<LinearLayout
安卓:id="@+id/lefthandmenu"
安卓:layout_width="85dip"
安卓:layout_height="match_parent"
安卓:layout_below="@id/title"
安卓:layout_alignParentLeft="true"
安卓:orientation="vertical"
安卓:background="@drawable/border_cut" >
<ImageView
... />
...
</LinearLayout>
<LinearLayout
安卓:id="@+id/righthandmenu"
安卓:layout_width="85dip"
安卓:layout_height="match_parent"
安卓:layout_below="@id/title"
安卓:layout_alignParentRight="true"
安卓:orientation="vertical"
安卓:background="@drawable/border_cut" >
<ImageView
... />
...
</LinearLayout>
<!-- Blank space which will contain other activities -->
<LinearLayout
安卓:id="@+id/activitycontent"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:layout_toLeftOf="@id/righthandmenu"
安卓:layout_toRightOf="@id/lefthandmenu"
安卓:layout_below="@id/title"
安卓:orientation="vertical" >
</LinearLayout>
这个类定义了所有图标的onClickListener
菜单视图。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));
}
};
好吧,有了这个(我不确定是否一切正常,也许我在充气方法或类似的东西上做错了),现在的问题是定义其他活动的布局在这个视图中。为此,我写下:
示例活动。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);
但我在最后一行得到一个NullPointerException。所以,在这段代码上膨胀时,一定是出了问题
# 1 楼答案
请使用以下结构。放在每个活动布局中包含您的通用布局
# 2 楼答案
您可以有一个像BaseActivity这样的主活动,它由FragmentActivity扩展。基本活动扩展了SlidingFragmentActivity并实现了菜单等基本结构。FragmentActivity onCreate方法为:
当您按下菜单中的一些按钮(左边框和右边框)时,您将在FragmentActivity中使用此功能更改屏幕中间的片段:
注
R.id.content_frame
是可在这两行之间切换的XML(比如两侧的菜单),而R.id.side_framgments
是可在主布局之间切换的两行