質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.30%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

465閲覧

navigationdrawerを非表示にし、それ以外のレイアウトを表示させたい

tera877

総合スコア9

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2023/08/04 06:36

実現したいこと

同じフラグメント内のnavigationdrawerだけを非表示にし、それ以外のレイアウトは表示させる設定にしたいです。

前提

navigationdrawerをactivityMain.xmlに記述し、すべての画面でnavigationdrawerが表示されるようにしました。しかし、特定のフラグメントではnavigationdrawerが表示されることが不都合になる場合もあるので、そういったフラグメントではnavigationdrawerを非表示にしたいです。
ところが、navigationdrawerを全体のコンテナとしているため、navigationdrawerを設定しているidを指定してView.GONEをつかって非表示にすると、コンテナ内のすべてのレイアウトが非表示になってしまい、何もない状態になってしまいます。navigationdrawerを実装するさいは、全体のコンテナとして設定することが推奨されていると聞いたので、レイアウトの設定は該当のソースコードのままにしていますが、そうするとどのようにして中のレイアウトだけを表示させればいいのでしょうか

詳しい方よろしくお願いいたします。

該当のソースコード

activityMain.xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.drawerlayout.widget.DrawerLayout 3 android:id="@+id/drawer_layout" 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 xmlns:tools="http://schemas.android.com/tools" 6 xmlns:app="http://schemas.android.com/apk/res-auto" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context=".MainActivity"> 10 11 <LinearLayout 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:orientation="horizontal"> 15 16 <androidx.appcompat.widget.Toolbar 17 android:id="@+id/toolbar" 18 android:layout_width="match_parent" 19 android:layout_height="?attr/actionBarSize" 20 android:background="@color/secondary" 21 android:elevation="10dp" /> 22 23 </LinearLayout> 24 25 <androidx.constraintlayout.widget.ConstraintLayout 26 android:id="@+id/constraint_layout" 27 android:layout_width="match_parent" 28 android:layout_height="match_parent" > 29 30 <androidx.fragment.app.FragmentContainerView 31 android:id="@+id/nav_host_fragment" 32 android:name="androidx.navigation.fragment.NavHostFragment" 33 app:defaultNavHost="true" 34 android:layout_width="match_parent" 35 android:layout_height="match_parent" 36 app:layout_constraintStart_toStartOf="parent" 37 app:layout_constraintEnd_toEndOf="parent" 38 app:layout_constraintTop_toTopOf="parent" 39 app:layout_constraintBottom_toBottomOf="parent" 40 app:navGraph="@navigation/nav_graph" /> 41 42 <com.google.android.material.bottomnavigation.BottomNavigationView 43 android:id="@+id/bottom_navigation_view" 44 android:layout_width="match_parent" 45 android:layout_height="wrap_content" 46 app:layout_constraintStart_toStartOf="parent" 47 app:layout_constraintBottom_toBottomOf="parent" 48 app:menu="@menu/bottom_navigation_menu"/> 49 50 </androidx.constraintlayout.widget.ConstraintLayout> 51 52 <!--NavigationDrawer--> 53 <com.google.android.material.navigation.NavigationView 54 android:id="@+id/navigation_drawer" 55 android:layout_width="wrap_content" 56 android:layout_height="match_parent" 57 android:layout_gravity="start" 58 app:menu="@menu/navigation_drawer" /> 59 60</androidx.drawerlayout.widget.DrawerLayout>

試したこと

表示させたくないフラグメントで、下記のコードのように、先にdrawerlayout全体を非表示にしてから、その後にコンテナ内のレイアウト(constraintLayout)を表示するといったコードを追加してみましたが、何も表示されませんでした。エラーにはなっていません。この記述をしているのはフラグメントのonViewCreatedの部分です

kotlin

1activity?.findViewById<View>(R.id.drawer_layout)?.visibility = View.GONE 2activity?.findViewById<ConstraintLayout>(R.id.constraint_layout)?.visibility = View.VISIBLE

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tera877

2023/08/06 00:42

ありがとうございます。無事解決することができました
guest

回答1

0

自己解決

Mainactivityのインスタンスをフラグメントで取得し、actionBarDrawerToggleを取得、その後、ナビゲーションドロワーを非表示にしたいフラグメントで.isDrawerIndicatorEnabled = falseをすると非表示になりました。逆に、表示させる際はtrueに設定します。
トップメニューを非表示にするには、同じようにツールバーのインスタンスを取得し、.visibility = View.GONEをすることで非表示になります。これは他のビューでも一緒ですね

投稿2023/08/06 00:45

tera877

総合スコア9

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問