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

时间:2013-10-31 11:37:18

标签: java android css android-layout

我有这个布局:

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

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

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

这是布局xml:

menu_view.xml

<RelativeLayout     
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
tools:context="com.example.MenuView" >


<!-- Show top black custom title bar-->
<include 
    layout="@layout/custom_tittlebar" >
</include>


<!-- Button columns -->    
<LinearLayout 
    android:id="@+id/lefthandmenu"
    android:layout_width="85dip"
    android:layout_height="match_parent"
    android:layout_below="@id/title"
    android:layout_alignParentLeft="true"
    android:orientation="vertical"       
    android:background="@drawable/border_cut" >

    <ImageView
        ... />
        ...
</LinearLayout>

<LinearLayout 
    android:id="@+id/righthandmenu"
    android:layout_width="85dip"
    android:layout_height="match_parent"
    android:layout_below="@id/title"
    android:layout_alignParentRight="true"
    android:orientation="vertical"
    android:background="@drawable/border_cut" >

    <ImageView
        ... />
        ...
</LinearLayout>

<!-- Blank space which will contain other activities -->
<LinearLayout
    android:id="@+id/activitycontent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toLeftOf="@id/righthandmenu"
    android:layout_toRightOf="@id/lefthandmenu"
    android:layout_below="@id/title"
    android:orientation="vertical" >    
</LinearLayout>   

这是定义所有图标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);

但是我在最后一行得到了NullPointerException。因此,在对这些片段进行充气时,某些内容必定是错误的。

2 个答案:

答案 0 :(得分:0)

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

<?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>

答案 1 :(得分:0)

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

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