質問編集履歴
5
情報変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,8 +21,6 @@
|
|
21
21
|
import android.view.MenuItem;
|
22
22
|
import android.widget.TextView;
|
23
23
|
|
24
|
-
import jp.techacademy.shoichi.ito.aft.R;
|
25
|
-
|
26
24
|
public class MainActivity extends AppCompatActivity {
|
27
25
|
|
28
26
|
private TextView mTextMessage;
|
4
写真追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -229,4 +229,8 @@
|
|
229
229
|
```
|
230
230
|
|
231
231
|
どこをどう変更して追加すると
|
232
|
-
エラーがなく正常に作動するのか、わかりませんmm
|
232
|
+
エラーがなく正常に作動するのか、わかりませんmm
|
233
|
+
|
234
|
+
エラーメッセージ
|
235
|
+
↓
|
236
|
+

|
3
例の追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,16 +1,19 @@
|
|
1
|
+
少し長くなります。また文章が
|
2
|
+
わからない箇所もあるかと思いますが
|
1
|
-
|
3
|
+
どうぞよろしくお願いいたします。
|
2
4
|
|
5
|
+
現在、android studioでショッピングサイトの作成をしております。
|
6
|
+
BottomNavigationViewにて作成しているのですが
|
3
|
-
アイコンの大きさを均等にする方法を教えていただきたいです。
|
7
|
+
下に表示されるアイコンの大きさを均等にする方法を教えていただきたいです。
|
4
8
|
|
5
|
-
|
9
|
+
写真のように[商品]だけが大きく表示されてしまいます。
|
6
10
|
|
7
|
-
|
8
|
-
|
9
11
|

|
10
12
|
|
11
|
-
|
13
|
+
当方が書いてきたコードは以下です。
|
14
|
+
MaimActivity.java
|
15
|
+
↓↓↓↓↓↓↓↓↓↓↓↓↓
|
12
16
|
|
13
|
-
|
14
17
|
```import android.os.Bundle;
|
15
18
|
import android.support.annotation.NonNull;
|
16
19
|
import android.support.design.widget.BottomNavigationView;
|
@@ -60,12 +63,13 @@
|
|
60
63
|
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
|
61
64
|
}
|
62
65
|
}
|
63
|
-
コード
|
64
66
|
```
|
65
67
|
|
66
68
|
|
67
|
-
レイアウトは以下二つ
|
68
69
|
|
70
|
+
|
71
|
+
Activity Main
|
72
|
+
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
|
69
73
|
```<?xml version="1.0" encoding="utf-8"?>
|
70
74
|
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
71
75
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
@@ -85,10 +89,13 @@
|
|
85
89
|
app:menu="@menu/navigation" />
|
86
90
|
|
87
91
|
</android.support.constraint.ConstraintLayout>
|
88
|
-
|
92
|
+
|
89
93
|
```
|
90
94
|
|
91
95
|
|
96
|
+
|
97
|
+
navigation.xml
|
98
|
+
|
92
99
|
```<?xml version="1.0" encoding="utf-8"?>
|
93
100
|
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
94
101
|
|
@@ -120,5 +127,106 @@
|
|
120
127
|
|
121
128
|
</menu>
|
122
129
|
|
123
|
-
コード
|
124
|
-
```
|
130
|
+
```
|
131
|
+
手順①
|
132
|
+
[4つ以上のアイテムを均等の大きさで並べる]
|
133
|
+
4つ以上のアイテムを均等に並べるにはレイアウトをごにょごにょする必要があるが、今回はBottomNavigationViewを継承してBottomNavigationViewの独自クラスをするのではなく
|
134
|
+
「BottomNavigationViewHelper」というヘルパークラスを作成してBottomNavigationViewを作るときにレイアウトを調整する。
|
135
|
+
|
136
|
+
BottomNavigationViewHelperを新規に作成する
|
137
|
+
こちらがwebからみつけだしたコードなのですが
|
138
|
+
どう変更したら良いか教え頂けますと幸いです。
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
```
|
143
|
+
package com.myapp.myapp;
|
144
|
+
|
145
|
+
import android.support.design.internal.BottomNavigationItemView;
|
146
|
+
import android.support.design.internal.BottomNavigationMenuView;
|
147
|
+
import android.support.design.widget.BottomNavigationView;
|
148
|
+
|
149
|
+
import java.lang.reflect.Field;
|
150
|
+
|
151
|
+
public class BottomNavigationViewHelper {
|
152
|
+
|
153
|
+
/**
|
154
|
+
* BottomNavigationViewのアイテムのサイズの調整、アイコンサイズ調整、タイトルの削除
|
155
|
+
*
|
156
|
+
* @param view
|
157
|
+
*/
|
158
|
+
public static void disableShiftMode(BottomNavigationView view) {
|
159
|
+
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
|
160
|
+
try {
|
161
|
+
|
162
|
+
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
|
163
|
+
shiftingMode.setAccessible(true);
|
164
|
+
shiftingMode.setBoolean(menuView, false);
|
165
|
+
shiftingMode.setAccessible(false);
|
166
|
+
|
167
|
+
for (int i = 0; i < menuView.getChildCount(); i++) {
|
168
|
+
|
169
|
+
/**
|
170
|
+
* アイテムの幅調整
|
171
|
+
*/
|
172
|
+
BottomNavigationItemView bottomNavigationItemView = (BottomNavigationItemView) menuView.getChildAt(i);
|
173
|
+
// noinspection RestrictedApi
|
174
|
+
bottomNavigationItemView.setShiftingMode(false);
|
175
|
+
// チェックされた値を設定すると、ビューが更新されるみたい
|
176
|
+
// noinspection RestrictedApi
|
177
|
+
bottomNavigationItemView.setChecked(false);
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
} catch (NoSuchFieldException e) {
|
182
|
+
e.printStackTrace();
|
183
|
+
} catch (IllegalAccessException e) {
|
184
|
+
e.printStackTrace();
|
185
|
+
}
|
186
|
+
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
```
|
191
|
+
|
192
|
+
手順②
|
193
|
+
|
194
|
+
|
195
|
+
```
|
196
|
+
ActivityからBottomNavigationViewHelperを呼びす
|
197
|
+
onCreateでBottomNavigationViewHelper#disableShiftModeへBottomNavigationViewを渡し調整、その後ついでに選択のリスナーを実装
|
198
|
+
|
199
|
+
// ボトムナビゲーションを読み込む
|
200
|
+
BottomNavigationView bottomavigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);
|
201
|
+
// BottomNavigationViewHelperでアイテムのサイズ、アニメーションを調整
|
202
|
+
BottomNavigationViewHelper.disableShiftMode(bottomavigation);
|
203
|
+
// BottomNavigationViewを選択したときのリスナー
|
204
|
+
bottomavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
|
205
|
+
@Override
|
206
|
+
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
207
|
+
|
208
|
+
// 各選択したときの処理
|
209
|
+
switch (item.getItemId()) {
|
210
|
+
case R.id.nav_camera:
|
211
|
+
|
212
|
+
return true;
|
213
|
+
case R.id.nav_gallery:
|
214
|
+
|
215
|
+
return true;
|
216
|
+
case R.id.nav_slideshow:
|
217
|
+
|
218
|
+
return true;
|
219
|
+
case R.id.nav_manage:
|
220
|
+
|
221
|
+
return true;
|
222
|
+
}
|
223
|
+
return false;
|
224
|
+
}
|
225
|
+
});
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
```
|
230
|
+
|
231
|
+
どこをどう変更して追加すると
|
232
|
+
エラーがなく正常に作動するのか、わかりませんmm
|
2
コード
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
全て「商品」と同じ表示にしたい
|
1
|
+
全て「商品」と同じ表示にしたい
|
body
CHANGED
@@ -6,4 +6,119 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-

|
9
|
+

|
10
|
+
|
11
|
+
メインコードは以下です。
|
12
|
+
|
13
|
+
|
14
|
+
```import android.os.Bundle;
|
15
|
+
import android.support.annotation.NonNull;
|
16
|
+
import android.support.design.widget.BottomNavigationView;
|
17
|
+
import android.support.v7.app.AppCompatActivity;
|
18
|
+
import android.view.MenuItem;
|
19
|
+
import android.widget.TextView;
|
20
|
+
|
21
|
+
import jp.techacademy.shoichi.ito.aft.R;
|
22
|
+
|
23
|
+
public class MainActivity extends AppCompatActivity {
|
24
|
+
|
25
|
+
private TextView mTextMessage;
|
26
|
+
|
27
|
+
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
|
28
|
+
= new BottomNavigationView.OnNavigationItemSelectedListener() {
|
29
|
+
|
30
|
+
@Override
|
31
|
+
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
32
|
+
switch (item.getItemId()) {
|
33
|
+
case R.id.navigation_product:
|
34
|
+
mTextMessage.setText(R.string.title_product);
|
35
|
+
return true;
|
36
|
+
case R.id.navigation_cart:
|
37
|
+
mTextMessage.setText(R.string.title_cart);
|
38
|
+
return true;
|
39
|
+
case R.id.navigation_delivery_status:
|
40
|
+
mTextMessage.setText(R.string.title_delivery_status);
|
41
|
+
return true;
|
42
|
+
case R.id.navigation_mypage:
|
43
|
+
mTextMessage.setText(R.string.title_mypage);
|
44
|
+
return true;
|
45
|
+
case R.id.navigation_other:
|
46
|
+
mTextMessage.setText(R.string.title_other);
|
47
|
+
return true;
|
48
|
+
}
|
49
|
+
return false;
|
50
|
+
}
|
51
|
+
};
|
52
|
+
|
53
|
+
@Override
|
54
|
+
protected void onCreate(Bundle savedInstanceState) {
|
55
|
+
super.onCreate(savedInstanceState);
|
56
|
+
setContentView(R.layout.activity_main);
|
57
|
+
|
58
|
+
mTextMessage = (TextView) findViewById(R.id.message);
|
59
|
+
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
|
60
|
+
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
|
61
|
+
}
|
62
|
+
}
|
63
|
+
コード
|
64
|
+
```
|
65
|
+
|
66
|
+
|
67
|
+
レイアウトは以下二つ
|
68
|
+
|
69
|
+
```<?xml version="1.0" encoding="utf-8"?>
|
70
|
+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
71
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
72
|
+
xmlns:tools="http://schemas.android.com/tools"
|
73
|
+
android:id="@+id/container"
|
74
|
+
android:layout_width="match_parent"
|
75
|
+
android:layout_height="match_parent"
|
76
|
+
tools:context=".MainActivity">
|
77
|
+
|
78
|
+
<android.support.design.widget.BottomNavigationView
|
79
|
+
android:id="@+id/navigation"
|
80
|
+
android:layout_width="0dp"
|
81
|
+
android:layout_height="59dp"
|
82
|
+
android:background="?android:attr/windowBackground"
|
83
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
84
|
+
app:layout_constraintRight_toRightOf="parent"
|
85
|
+
app:menu="@menu/navigation" />
|
86
|
+
|
87
|
+
</android.support.constraint.ConstraintLayout>
|
88
|
+
コード
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
```<?xml version="1.0" encoding="utf-8"?>
|
93
|
+
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
94
|
+
|
95
|
+
<item
|
96
|
+
android:id="@+id/navigation_product"
|
97
|
+
android:icon="@drawable/ic_home_black_24dp"
|
98
|
+
android:title="商品" />
|
99
|
+
|
100
|
+
<item
|
101
|
+
android:id="@+id/navigation_cart"
|
102
|
+
android:icon="@drawable/ic_shopping_cart_black_24dp"
|
103
|
+
android:title="カート" />
|
104
|
+
|
105
|
+
<item
|
106
|
+
android:id="@+id/navigation_delivery_status"
|
107
|
+
android:icon="@drawable/ic_local_shipping_black_24dp"
|
108
|
+
android:title="配送状況" />
|
109
|
+
|
110
|
+
<item
|
111
|
+
android:id="@+id/navigation_mypage"
|
112
|
+
android:icon="@drawable/ic_person_black_24dp"
|
113
|
+
android:title="マイページ" />
|
114
|
+
|
115
|
+
<item
|
116
|
+
android:id="@+id/navigation_other"
|
117
|
+
android:icon="@drawable/ic_settings_black_24dp"
|
118
|
+
android:title="その他" />
|
119
|
+
|
120
|
+
|
121
|
+
</menu>
|
122
|
+
|
123
|
+
コード
|
124
|
+
```
|
1
タイトル
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
全て「商品」と同じ表示にしたい...
|
body
CHANGED
File without changes
|