簡體   English   中英

在其他布局的LinearLayout內部膨脹一個布局

[英]Inflate a layout inside other layout's LinearLayout

我有這個布局:

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方法或類似的方法做錯了什么),現在的事情是將其他活動的布局定義在此視圖內。 為此,我寫道:

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。 因此,誇大這些片段時一定是錯誤的。

嘿,使用以下結構。 放。 在每個活動布局中包括您的通用布局

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

您可以擁有一個由FragmentActivity擴展的主要活動,例如BaseActivity。 基本活動擴展了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是可在主布局之間切換的這兩行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM