2

アンドロイドのレイアウトファイルがうまくいかず、ツールバーウィジェットとリサイクラービューウィジェットがあるファイルがあります。ここにコードがあります。ナビゲーションドロワー、ツールバー、およびリサイクラーすべて同じxmlレイアウトファイルで表示しますか?

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/robot_chooser_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/robot_recycler_view" 
     android:paddingTop="5dp" 
     android:scrollbars="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <TextView 
     android:id="@+id/robot_empty_view" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textSize="20sp" 
     android:text="@string/no_robots" 
     android:elevation="3dp" 
     android:layout_gravity="center" 
     android:visibility="gone" 
     android:gravity="center" /> 

</LinearLayout> 

これにDrawerLayoutを追加するにはどうすればよいですか?

答えて

5

DrawerLayoutは二つの部分

  1. メインコンテンツを持って
  2. ナビゲーションドロワー

は、言い換えれば、それは2つのつの子ビュー、MainContentなどでの一幕とDrawerなどの他の行為が含まれています。あなたがあなたのケースでは、単純なビュー

<DrawerLayout> 

    // VIEW 1 - MAIN CONTENT 
    <RelativeLayout> 

     //whatever you put in this, it will show in your main Content, 
     // main Screen 

    </RelativeLayout> 

    //SECOND CHILD any view, it would be seen in your Side drawer 

    <LinearLayout> 

     //whatever you put inside, this will be seen on your drawerlayout 

    </LinearLayout> 
</DrawerLayout> 

enter image description here


を見れば は、次のようにあなたが行うことができます 、あなたのDrawer内のフラグメントを追加したい、とします

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:id="@+id/profileDrawer" 
android:layout_height="match_parent" > 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/robot_chooser_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:elevation="4dp" 
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/robot_recycler_view" 
    android:paddingTop="5dp" 
    android:scrollbars="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

<TextView 
    android:id="@+id/robot_empty_view" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:textSize="20sp" 
    android:text="@string/no_robots" 
    android:elevation="3dp" 
    android:layout_gravity="center" 
    android:visibility="gone" 
    android:gravity="center" /> 

</LinearLayout> 

<fragment 
    android:layout_width="290dp" 
    android:layout_gravity="end" 
    android:layout_height="match_parent" 
    android:name="yourFragmentPackageName" 
    android:id="@+id/fragment" 
    tools:layout="@layout/drawer_fragment" /> 

</android.support.v4.widget.DrawerLayout> 
+0

あなたのお返事ありがとうございます!一番下の断片部分は何ですか? –

+0

何かで置き換えることができます。上で説明したように、 'DrawerLayout'には2つの子があるので、' View'で置き換えることができます。私の例では、説明のために 'Fragment'を追加しました。あなたは任意のViewを追加することができます。 –

+0

ナビゲーション・ドロワの内側に追加しようとしているのは断片的なものだからです。したがって、そこにある2つのものの1つをクリックすると、フラグメントがドロワの中にフラグメントを追加したい場合は、フラグメント –

関連する問題