充气内的其他布局的的LinearLayout布局 [英] Inflate a layout inside other layout's LinearLayout

查看:131
本文介绍了充气内的其他布局的的LinearLayout布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的布局:

在2边界(左,右)由图标填充。当我触摸其中一个图标我访问等活动。顶黑酒吧是一个自定义标题栏。

内部空间的清灰是我需要适应,我已经得到了我的应用程序的所有活动。因此,这种布局将类似的菜单布局,这将是静态的所有活动。

这是布局的xml:

menu_view.xml

 < RelativeLayout的
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=com.example.MenuView>


<! - 显示顶级黑自定义标题栏 - >
<包括
    布局=@布局/ custom_tittlebar>
< /包括>


<! - 巴顿列 - >
<的LinearLayout
    机器人:ID =@ + ID / lefthandmenu
    机器人:layout_width =85dip
    机器人:layout_height =match_parent
    机器人:layout_below =@ ID /标题
    机器人:layout_alignParentLeft =真
    机器人:方向=垂直
    机器人:背景=@可绘制/ border_cut>

    < ImageView的
        ... />
        ...
< / LinearLayout中>

<的LinearLayout
    机器人:ID =@ + ID / righthandmenu
    机器人:layout_width =85dip
    机器人:layout_height =match_parent
    机器人:layout_below =@ ID /标题
    机器人:layout_alignParentRight =真
    机器人:方向=垂直
    机器人:背景=@可绘制/ border_cut>

    < ImageView的
        ... />
        ...
< / LinearLayout中>

<! - 空白空间,将包含其他活动 - >
<的LinearLayout
    机器人:ID =@ + ID / activitycontent
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_toLeftOf =@ ID / righthandmenu
    机器人:layout_toRightOf =@ ID / lefthandmenu
    机器人:layout_below =@ ID /标题
    机器人:方向=垂直>
< / LinearLayout中>
 

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

MenuView.java

 公共类MenuView扩展RelativeLayout的{

私人最终LayoutInflater充气;
上下文语境;

公共MenuViewActivity(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);

    this.context =背景;
    充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.menu_view,这一点,真正的);


    ((ImageView的)this.findViewById(R.id.navButton))setOnClickListener(launch_nav)。
}

最后OnClickListener launch_nav =新OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        。的getContext()startActivity(新意图(的getContext(),Navigation.class));
    }
};
 

嗯,有这个(我不知道如果一切OK,也许我做错事的充气法或类似这样的东西),现在的事情是确定的其他活动中的的布局是这里面视图。要做到这一点,我写的:

ExampleActivity.java

 公共类为ExampleActivity延伸活动{

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    的LinearLayout inside_menu_view =(的LinearLayout)findViewById(R.id.activitycontent);

    查看this_layout = getLayoutInflater()膨胀(R.layout.main,inside_menu_view,真正的);
    inside_menu_view.addView(this_layout);
 

但我发现了在这最后一行一个NullPointerException异常。因此,在这个片段充气东西的时候一定是错误的。

解决方案

您可以像BaseActivity由FragmentActivity延长一个主活动。基本活动来延长SlidingFragmentActivity并实现了基本的结构是怎样的菜单等FragmentActivity onCreate方法是:

  

@覆盖

 公共无效的onCreate(包savedInstanceState){
                      super.onCreate(savedInstanceState);

                      FM = getSupportFragmentManager();
                      //这是可切换的主要内容
                      mContent =新MainListActivity();


                       //另外,你可以定义这两条线的片段并将其添加为默认的两边:
                      sideFragments =新sideFragments(本);
                      FragmentTransaction英尺= fm.beginTransaction();
                      ft.replace(R.id.content_frame,mContent);
                      ft.replace(R.id.side_framgments,sideFragments);
                      ft.commit();
                  }
 

在preSS一些从你的菜单这些按钮(左,右边框),你会改变片段在屏幕中间用中FragmentActivity此功能:

 公共无效switchContent(片段片段,字符串变量){
        mContent =片段;
        getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,fragment).addToBackStack(tag).commit();


  }
 

请注意 R.id.content_frame 是这两条线之间的切换(让说双方菜单)和 R.id的XML .side_framgments 在主布局可切换之间的这两条线。

I've got this Layout:

The 2 borders (left,right )are filled by icons. When I touch one of these icons I access to other activity. The top black bar is a custom title bar.

The clear grey inside space is where I need to fit all activities that I've got on my app. So this layout would be something like a menu layout that would be static in all the activities.

This is the Layout 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>   

And this is the class where are defined all the icon's 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));
    }
};

Well, having this (I'm not sure if its all ok, maybe I'm doing something wrong with the inflate method or something like this), now the thing would be to define the other activitie's layouts to be inside this view. To do this, I write:

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);

But I'm getting a NullPointerException on this last line. So, something when inflating on this snippets must be wrong.

解决方案

You can have one main activity like BaseActivity that is extended by FragmentActivity. Base activity extends SlidingFragmentActivity and implements basic structure like menu etc. FragmentActivity onCreate method is :

@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();
                  }

When you press some of those buttons from your menu(left and right border) you will change the fragment in middle of the screen with this function in FragmentActivity :

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


  }

Note R.id.content_frame is the XML that is switchable between those two lines (let say menus on both sides) and R.id.side_framgments are those two lines in between the main layout that is switchable.

这篇关于充气内的其他布局的的LinearLayout布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆