将导航抽屉添加到左上角

时间:2016-04-18 08:13:05

标签: android eclipse layout navigation

我想在屏幕的左上角添加一个导航抽屉,其中包含所有按钮,这样您就可以在另一个活动中浏览所有活动。

我不知道如何开始这个,有人能帮我一把吗?

这是我的代码到目前为止, 主:

<form onsubmit="return jsonpost(this);" method=POST action=/validate/>
    <label>Firstname: <input required name=firstName></label>
    <label>Lastname: <input required name=lastName></label>
    <label>Age: <input name=age type=number></label>
    <input type=submit>
<form>

<script>
function jsonpost(jsonpostform) {

    // collect the jsonpostform data while iterating over the inputs
    var data = {};
    for (var i = 0; i < jsonpostform.length; i++) {
        var input = jsonpostform[i];
        if (input.name && input.value) {
            data[input.name] = input.value;
        }
    }

    // construct an HTTP request
    var xhr = new XMLHttpRequest();
    xhr.open(jsonpostform.method, jsonpostform.action);
    xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
    // send the collected data as JSON
    xhr.send(JSON.stringify(data));

    return false;
}
</script>

activity_main:

package com.example.rodekruis;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener{

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

        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
        findViewById(R.id.button6).setOnClickListener(this);
        findViewById(R.id.button7).setOnClickListener(this);
        findViewById(R.id.button8).setOnClickListener(this);
        findViewById(R.id.button9).setOnClickListener(this);
        findViewById(R.id.button10).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {

            case R.id.button10:
                intent = new Intent(MainActivity.this, AfspraakActivity.class);
                break;
            case R.id.button8:
                intent = new Intent(MainActivity.this, BezoekActivity.class);
                break;
            case R.id.button9:
                intent = new Intent(MainActivity.this, ContactActivity.class);
                break;
            case R.id.button3:
                intent = new Intent(MainActivity.this, MeningActivity.class);
                break;
            case R.id.button4:
                intent = new Intent(MainActivity.this, RouteActivity.class);
                break;
            case R.id.button1:
                intent = new Intent(MainActivity.this, SpecialistenActivity.class);
                break;
            case R.id.button5:
                intent = new Intent(MainActivity.this, BWCActivity.class);
                break;
            case R.id.button6:
                intent = new Intent(MainActivity.this, AgendaActivity.class);
                break;
            case R.id.button7:
                intent = new Intent(MainActivity.this, NieuwsActivity.class);
                break;
        }
        startActivity(intent);
    }
}

1 个答案:

答案 0 :(得分:0)

要创建导航抽屉,您需要将DrawerLayout用作视图的根目录。请注意,gravity负责导航抽屉的对齐。它以你想要在左边显示的开头给出。

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

之后,您可以使用按钮初始化listview

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mButtonTitles))

完成后,定义onclick侦听器并打开相应的活动。