Java 如何为导航抽屉活动使用自己的布局,而不是自动生成的布局?

Java 如何为导航抽屉活动使用自己的布局,而不是自动生成的布局?,java,android,android-activity,navigation-drawer,Java,Android,Android Activity,Navigation Drawer,我有一个导航抽屉活动,它自动生成了这些活动(学生家庭)2,应用程序(酒吧)(学生家庭),内容(学生家庭),导航头(学生家庭)布局文件。我已经单独创建了活动学生家庭布局。默认情况下活动java类的活动学生家庭2布局为设置内容视图。但是,我想使用acitivity\u student\u home布局如果我尝试将setContentView更改为acitivity\u student\u home,则我的导航抽屉视图不起作用。如何使用acitivity\u student\u home布局作为set

我有一个
导航抽屉活动
,它自动生成了这些
活动(学生家庭)2
应用程序(酒吧)(学生家庭)
内容(学生家庭)
导航头(学生家庭)
布局文件。我已经单独创建了
活动学生家庭
布局。

默认情况下
活动java类
活动学生家庭2
布局为
设置内容视图
。但是,我想使用
acitivity\u student\u home
布局

如果我尝试将
setContentView
更改为
acitivity\u student\u home
,则我的
导航抽屉视图不起作用。如何使用
acitivity\u student\u home
布局作为
setContentView


活动\学生\家庭布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_back"
    tools:context="bd.edu.bubt.regup.StudentHomeActivity">

    <LinearLayout
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="false"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="60dp"
        android:id="@+id/linearLayout">

        <TextView
            android:id="@+id/showtext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="300dp"
            android:gravity="center"
            android:text="Demo"
            android:textSize="24dp"
            android:textStyle="bold" />

    </LinearLayout>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_student_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_student_home"
        app:menu="@menu/activity_student_home2_drawer" />

</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_back"
    tools:context="bd.edu.bubt.regup.StudentHomeActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_student_home" />

</android.support.design.widget.CoordinatorLayout>
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class StudentHomeActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    String name, id, dept, intake, sec, mail;

    TextView showtext;

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

        showtext = (TextView) findViewById(R.id.showtext); //content of activity_student_home
        showtext.setText("Registration is open"); //does not work. Null pointer exception.

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.menu_bvault) {
            // Handle the camera action
        } else if (id == R.id.menu_account) {

        } else if (id == R.id.menu_pass_change) {

        } else if (id == R.id.menu_logout) {

        } else if (id == R.id.menu_about) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

做你想做的事情的正确方法是(如果我正确理解你的要求)使用片段

在您的应用程序中\u bar\u student\u home.xml替换

<include layout="@layout/content_student_home" />
事情远不止这些

例如,当您希望将单击事件从片段发送到活动时,必须使用getActivity()并将其强制转换到MainActivity的类或使用接口(最好)

您可以查看本教程,本教程将对您有所帮助


您的
活动\u学生之家
不包括您的抽屉布局

这个不见了

<include
    layout="@layout/app_bar_student_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


否。我不想使用片段。我只是想处理活动\学生\家庭布局内容。尤其是文本视图。如果我了解您正在尝试做什么,请尝试更改,因为您现在有活动\学生\家庭从未显示,也从未充气,因此您无法处理它的文本视图,因为它不在那里。为什么它如此重要?是否要动态更改视图?活动正在运行时?我希望我的导航视图能够处理活动以及活动\学生\主页的内容。
getSupportFragmentManager().beginTransaction().replace(R.id.content, new FragmentA()).commit();
<include
    layout="@layout/app_bar_student_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />