有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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。所以,在这段代码上膨胀时,一定是出了问题


共 (2) 个答案

  1. # 1 楼答案

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

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
    
    <!  Show top black custom title bar >
    <include 
        android:id="@+id/ll_top"
        layout="@layout/custom_tittlebar"
        android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" >
    </include>
    
    
    
    <include 
        android:id="@+id/ll_left"
        layout="@layout/custom_left"
        android:layout_alignParentLeft="true"
    android:layout_centerVertical="true" >
    
    
    </include>
    <include 
        android:id="@+id/ll_right"
        layout="@layout/custom_right"
        android:layout_alignParentRight="true"
    android:layout_centerVertical="true" >
    </include>
    
    <Button
        android:id="@+id/btn_center"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="center"
        android:layout_toRightOf="@+id/ll_left"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/ll_top"
        ></Button>
    </RelativeLayout>
    
  2. # 2 楼答案

    您可以有一个像BaseActivity这样的主活动,它由FragmentActivity扩展。基本活动扩展了SlidingFragmentActivity并实现了菜单等基本结构。FragmentActivity onCreate方法为:

    @Override

    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中使用此功能更改屏幕中间的片段:

      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是可在主布局之间切换的两行