我試圖將抽屜添加到我的應用程式中的一項活動中。但是,導航視圖涵蓋了所有其他布局,這是我打開 Activity 時唯一能看到的內容。我無法將其滑回,或對其進行任何操作。其他物品似乎以某種方式放在那個抽屜后面。對不起,如果這很簡單,這是我第一次這樣做。有誰知道為什么會發生這種情況?
這是我的 XML 代碼:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".CategoryActivity"
android:id="@ id/drawer_layout"
android:fitsSystemWindows="true"
android:background="@color/dark_blue"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu"
android:foregroundGravity="right"
>
</com.google.android.material.navigation.NavigationView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/dark_blue">
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="@ id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categories"
android:textColor="#ffffff"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginStart="130dp"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:src="@drawable/hist_bg" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="40dp"
android:gravity="end"
android:text="History"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@ id/bt_hist_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="270dp"
android:layout_marginTop="100dp"
android:background="@drawable/his_btn" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:src="@drawable/android_bg" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:gravity="start"
android:text="Programming"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginStart="20dp"/>
<Button
android:id="@ id/bt_prog_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="100dp"
android:background="@drawable/an_btn" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:src="@drawable/allknow_bg" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="75dp"
android:layout_marginEnd="40dp"
android:gravity="end"
android:text="Technology"
android:textColor="@color/white"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@ id/bt_tech_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="270dp"
android:layout_marginTop="100dp"
android:background="@drawable/allknow_btn" />
</RelativeLayout>
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
這是活動代碼:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = 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();
uj5u.com熱心網友回復:
我可以在您的 xml 代碼中看到一些遺漏的東西。
<AppBarLayout您的標簽沒有父<Toolbar標簽- NavigationView 的目的是什么?你沒有給它任何ID并且沒有使用它?
- NavigationView
height應該是match_parent foregroundGravity在 NavigationView 中洗掉并添加android:layout_gravity="start",同時添加android:fitsSystemWindows="true"
你應該初始化你NavigationView的Main Activity并用它來設定NavController。我不會給你所有的代碼,所以你可以自己搜索一下,但如果你沒有找到解決方案,請告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/494803.html
上一篇:使用SQLServerFORXML創建可擴展的XML檔案
下一篇:AAPT:錯誤:“0”與屬性layout_constraintEnd_toEndOf(attr)參考不兼容|列舉[parent=0]
