如何构建导航抽屉?

时间:2016-06-19 12:25:24

标签: android xml android-fragments

我试图学习导航抽屉,但很难学习<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- The navigation drawer --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" android:background="#111"/> </android.support.v4.widget.DrawerLayout>

在Android网站上,它说了

  

FrameLayout包含主要内容(由Fragment填充   运行时)

我如何在main_content中包含多个片段?假设我有片段A,B和C

实施例

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="#8b0404"
    android:gravity="start">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@drawable/logo"
        android:layout_alignTop="@+id/fragment"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="150dp"
        />

</RelativeLayout>

碎片;

async

2 个答案:

答案 0 :(得分:1)

   <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<fragment .../>
<fragment .../>
....
</FrameLayout>

FrameLayout只是一个容器 - 它可能包含您想要的任何内容。

答案 1 :(得分:1)

在activity.xml中使用以下代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include layout="@layout/toolbar" />

<android.support.v4.widget.DrawerLayout
    android:layout_width="match_parent"
    android:id="@+id/drawerLayout"
    android:layout_height="match_parent">

    <!-- activity view -->

    <!-- navigation drawer -->
    <RelativeLayout
        android:layout_gravity="left|start"
        android:layout_width="240dp"
       android:layout_height="match_parent">

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:background="#e8e5e5"
            android:layout_height="match_parent"
            android:divider="#eee"
            android:dividerHeight="1dp" />
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

在Activity.java中使用这个。

public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ListView leftDrawerList;
private ArrayAdapter<String> navigationDrawerAdapter;
private String[] leftSliderData =
    {"item2",
    "item 2",
       .
       .
       .

   };

    private Integer[] imageId = {
    R.drawable.ic_1,
    R.drawable.ic_2,
    R.drawable.ic_3,
    R.drawable.ic_4,
    R.drawable.ic_5
    };

    @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    leftDrawerList = (ListView) findViewById(R.id.left_drawer);
    CustomList adapter = new CustomList(MainActivity.this, leftSliderData,        imageId);

toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
  leftDrawerList.setAdapter(adapter);

leftDrawerList.setSelector(R.drawable.list_bg);

leftDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
      //  Toast.makeText(MainActivity.this, "You Clicked at " +leftSliderData[+ position], Toast.LENGTH_SHORT).show();

        if (position == 0) {

            Intent in = new Intent(MainActivity.this, Activity1.class);
            startActivity(in);

        }

        if (position == 1) {

            Intent in = new Intent(MainActivity.this, Activity2.class);
            startActivity(in);

        }

        if(position == 2){

            Intent in = new Intent(MainActivity.this, Activity3.class);
            startActivity(in);

        }
        if(position == 3){

            Intent in = new Intent(MainActivity.this, Activity4.class);
            startActivity(in);

        }
        if(position == 4){

            Intent inw = new Intent(MainActivity.this, Activity5.class);
            startActivity(in);

        }

        if(position == 5){

            Intent in = new Intent(MainActivity.this, Activity6.class);
            startActivity(in);

        }
    }
});
 if (toolbar != null) {
    toolbar.setTitle("");
    setSupportActionBar(toolbar);
    toolbar.setLogo(R.drawable.small_logo);
}


drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

    @Override
    public void onDrawerClosed(View drawerView) {
        super.onDrawerClosed(drawerView);

    }

    @Override
    public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);

    }
};
drawerLayout.setDrawerListener(drawerToggle);

}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    onBackPressed();
    return true;
        }

@Override
public void onBackPressed() {

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

}

}