質問編集履歴
1
タイトルと文章修正、画像削除、コード追加
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
AndroidでNavigation Drawer
|
1
|
+
AndroidでNavigation Drawerで出てくるようなメニューを最初から配置させたい
|
body
CHANGED
@@ -2,18 +2,166 @@
|
|
2
2
|
|
3
3
|
タブレット(SM-T800)で使えるようにアプリをVisualStudio2015でxamarinを導入し、c#で制作しています。
|
4
4
|
新規作成から「Android Navigation Drawer App AppCompat」を選択し、タブレット用にレイアウトを追加した以外はそのままの状態です。
|
5
|
-
デフォルトで
|
5
|
+
デフォルトでデバッグすると画面左端からスワイプすることでナビゲーションメニューが出てくるのですが、
|
6
|
+
スワイプの必要なく、最初から表示している状態にしたいのです。
|
6
7
|
具体的には「Todoist」や「Evernote」みたいな感じです。スマートフォンだとスワイプでメニューが出てきますが、タブレットだと最初から表示されています。
|
7
8
|
|
8
|
-
タブレットの場合のUI
|
9
|
+
タブレットの場合のUI(画像の例はTodoist)
|
9
10
|

|
10
11
|
|
11
12
|
|
12
|
-
|
13
|
+
上記画像のようなUIを作りたいのですが、改変する場所の見当がつきません。
|
13
|
-
|
14
|
+
タブレットでも今のgoogle playみたいな感じで左から引っ張ってこないとメニューが出ない画面が適用されてしまいます。
|
14
|
-
|
15
|
+
最初からメニューを配置させたいのですが、どのようにすればよいでしょうか。
|
15
|
-

|
16
16
|
|
17
|
+
コード(main.axml)
|
18
|
+
|
19
|
+
```
|
20
|
+
|
21
|
+
<?xml version="1.0" encoding="utf-8"?>
|
22
|
+
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
23
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
24
|
+
android:id="@+id/drawer_layout"
|
25
|
+
android:layout_width="match_parent"
|
26
|
+
android:layout_height="match_parent">
|
27
|
+
<!-- The main content view -->
|
28
|
+
<RelativeLayout
|
29
|
+
android:layout_width="match_parent"
|
30
|
+
android:layout_height="match_parent">
|
31
|
+
<android.support.design.widget.AppBarLayout
|
32
|
+
android:layout_height="wrap_content"
|
33
|
+
android:layout_width="match_parent"
|
34
|
+
android:id="@+id/toolbar_layout">
|
35
|
+
<include
|
36
|
+
android:id="@+id/toolbar"
|
37
|
+
layout="@layout/toolbar"
|
38
|
+
app:layout_scrollFlags="scroll|enterAlways" />
|
39
|
+
</android.support.design.widget.AppBarLayout>
|
40
|
+
<FrameLayout
|
41
|
+
android:id="@+id/content_frame"
|
42
|
+
android:layout_below="@id/toolbar_layout"
|
43
|
+
android:layout_width="match_parent"
|
44
|
+
android:layout_height="match_parent" />
|
45
|
+
</RelativeLayout>
|
46
|
+
<android.support.design.widget.NavigationView
|
47
|
+
android:id="@+id/nav_view"
|
48
|
+
android:layout_width="400dp"
|
49
|
+
android:layout_height="match_parent"
|
50
|
+
android:layout_gravity="left"
|
51
|
+
app:headerLayout="@layout/nav_header"
|
52
|
+
app:menu="@menu/nav_menu" />
|
53
|
+
</android.support.v4.widget.DrawerLayout>
|
54
|
+
|
55
|
+
```
|
56
|
+
コード(MainActivity.cs)
|
57
|
+
```
|
58
|
+
using Android.App;
|
59
|
+
using Android.Content.PM;
|
60
|
+
using Android.Content.Res;
|
61
|
+
using Android.OS;
|
62
|
+
using Android.Support.V4.Widget;
|
63
|
+
using Android.Views;
|
64
|
+
using Android.Widget;
|
65
|
+
|
66
|
+
using App4.Fragments;
|
67
|
+
using Android.Support.V7.App;
|
68
|
+
using Android.Support.V4.View;
|
69
|
+
using Android.Support.Design.Widget;
|
70
|
+
|
71
|
+
namespace App4.Activities
|
72
|
+
{
|
73
|
+
[Activity(Label = "Home", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, Icon = "@drawable/Icon")]
|
74
|
+
public class MainActivity : BaseActivity
|
75
|
+
{
|
76
|
+
|
77
|
+
DrawerLayout drawerLayout;
|
78
|
+
NavigationView navigationView;
|
79
|
+
|
80
|
+
protected override int LayoutResource
|
81
|
+
{
|
82
|
+
get
|
83
|
+
{
|
84
|
+
return Resource.Layout.main;
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
protected override void OnCreate(Bundle savedInstanceState)
|
89
|
+
{
|
17
|
-
|
90
|
+
base.OnCreate(savedInstanceState);
|
91
|
+
|
92
|
+
|
18
|
-
|
93
|
+
drawerLayout = this.FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
|
94
|
+
|
19
|
-
|
95
|
+
//Set hamburger items menu
|
96
|
+
SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);
|
97
|
+
|
98
|
+
//setup navigation view
|
99
|
+
navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
|
100
|
+
|
101
|
+
//handle navigation
|
102
|
+
navigationView.NavigationItemSelected += (sender, e) =>
|
103
|
+
{
|
104
|
+
e.MenuItem.SetChecked(true);
|
105
|
+
|
106
|
+
switch (e.MenuItem.ItemId)
|
107
|
+
{
|
108
|
+
case Resource.Id.nav_home_1:
|
109
|
+
ListItemClicked(0);
|
110
|
+
break;
|
111
|
+
case Resource.Id.nav_home_2:
|
112
|
+
ListItemClicked(1);
|
113
|
+
break;
|
114
|
+
}
|
115
|
+
|
116
|
+
Snackbar.Make(drawerLayout, "You selected: " + e.MenuItem.TitleFormatted, Snackbar.LengthLong)
|
117
|
+
.Show();
|
118
|
+
|
119
|
+
drawerLayout.CloseDrawers();
|
120
|
+
};
|
121
|
+
|
122
|
+
|
123
|
+
//if first time you will want to go ahead and click first item.
|
124
|
+
if (savedInstanceState == null)
|
125
|
+
{
|
126
|
+
ListItemClicked(0);
|
127
|
+
}
|
128
|
+
}
|
129
|
+
|
130
|
+
int oldPosition = -1;
|
131
|
+
private void ListItemClicked(int position)
|
132
|
+
{
|
133
|
+
//this way we don't load twice, but you might want to modify this a bit.
|
134
|
+
if (position == oldPosition)
|
135
|
+
return;
|
136
|
+
|
137
|
+
oldPosition = position;
|
138
|
+
|
139
|
+
Android.Support.V4.App.Fragment fragment = null;
|
140
|
+
switch (position)
|
141
|
+
{
|
142
|
+
case 0:
|
143
|
+
fragment = Fragment1.NewInstance();
|
144
|
+
break;
|
145
|
+
case 1:
|
146
|
+
fragment = Fragment2.NewInstance();
|
147
|
+
break;
|
148
|
+
}
|
149
|
+
|
150
|
+
SupportFragmentManager.BeginTransaction()
|
151
|
+
.Replace(Resource.Id.content_frame, fragment)
|
152
|
+
.Commit();
|
153
|
+
}
|
154
|
+
|
155
|
+
public override bool OnOptionsItemSelected(IMenuItem item)
|
156
|
+
{
|
157
|
+
switch (item.ItemId)
|
158
|
+
{
|
159
|
+
case Android.Resource.Id.Home:
|
160
|
+
drawerLayout.OpenDrawer(Android.Support.V4.View.GravityCompat.Start);
|
161
|
+
return true;
|
162
|
+
}
|
163
|
+
return base.OnOptionsItemSelected(item);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
}
|
167
|
+
```
|