【问题标题】:How to use own layout for navigation drawer activity instead of auto generated layout?如何为导航抽屉活动使用自己的布局而不是自动生成的布局?
【发布时间】:2019-04-19 09:34:24
【问题描述】:

我有一个navigation drawer activity,它自动生成了这些activity_student_home2app_bar_student_homecontent_student_homenav_header_student_home 布局文件。我已经单独创建了 activity_student_home 布局。

默认情况下,activity java classacitivity_student_home2 布局为 setContentView。但相反,我想使用 acitivity_student_home 布局。

如果我尝试将 setContentView 更改为 acitivity_student_home,那么我的 navigation drawer view 将不起作用。如何将acitivity_student_home 布局用作setContentView


activity_student_home 布局:

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


activity_student_home2 布局:

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


app_bar_student_home 布局:

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


StudentHomeActivity java 类:

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

【问题讨论】:

    标签: java android android-activity navigation-drawer


    【解决方案1】:

    您的 activity_student_home 不包括您的 DrawerLayout

    这个不见了

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

    【讨论】:

    • 为什么如此重要?
    • 您想动态更改视图吗?活动正在运行时?
    • 我想要的是我的导航视图来处理活动以及活动的内容。
    【解决方案2】:

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

    在你的 app_bar_student_home.xml 里面替换

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

    类似这样的东西

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_height="match_parent"/>
    

    然后创建您希望显示其内容的片段(例如 FragmentA、FragmentB)并将它们的 contentview 设置为您希望显示的不同布局

    当您希望更改活动的内容时,您可以执行以下操作

    getSupportFragmentManager().beginTransaction().replace(R.id.content, new FragmentA()).commit();
    

    还有很多事情要做,

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

    你可以看看这个教程,它应该对你有帮助

    http://www.androiddocs.com/training/basics/fragments/index.html

    【讨论】:

    • 没有。我不想使用片段。我只想处理 activity_student_home 布局内容。尤其是 Textview。
    • 如果我了解您要做什么,请尝试将 更改为 ,因为您有它现在 activity_student_home 从未显示过,也从未膨胀过,因此您无法处理它的 textview,因为它不存在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    相关资源
    最近更新 更多