亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何為導航抽屜活動使用自己的布局而不是自動生成的布局?

如何為導航抽屜活動使用自己的布局而不是自動生成的布局?

料青山看我應如是 2022-10-12 15:58:27
我有一個navigation drawer activity, 自動生成這些activity_student_home2, app_bar_student_home, content_student_home,nav_header_student_home布局文件。我已經單獨創建了activity_student_home布局。默認情況下activity java class,acitivity_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">
查看完整描述

2 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

你activity_student_home不包括你的 DrawerLayout


這個不見了


<include

    layout="@layout/app_bar_student_home"

    android:layout_width="match_parent"

    android:layout_height="match_parent" />


查看完整回答
反對 回復 2022-10-12
?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

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


在你的 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


查看完整回答
反對 回復 2022-10-12
  • 2 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號