teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

サンプルソース訂正

2017/10/22 13:02

投稿

keicha_hrs
keicha_hrs

スコア6768

answer CHANGED
@@ -36,6 +36,7 @@
36
36
 
37
37
  ```
38
38
  activity_main.xml
39
+ (10/22 22:01 おかしな記述があったので訂正。結果に影響しないことは確認済。)
39
40
  ```XML
40
41
  <?xml version="1.0" encoding="utf-8"?>
41
42
  <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
@@ -43,7 +44,6 @@
43
44
  xmlns:tools="http://schemas.android.com/tools"
44
45
  android:layout_width="match_parent"
45
46
  android:layout_height="match_parent"
46
- android:orientation="vertical"
47
47
  tools:context=".MainActivity">
48
48
 
49
49
  <android.support.v7.widget.Toolbar

2

LinearLayout版

2017/10/22 13:01

投稿

keicha_hrs
keicha_hrs

スコア6768

answer CHANGED
@@ -55,7 +55,7 @@
55
55
  android:layout_marginTop="0dp"
56
56
  android:background="?attr/colorPrimary"
57
57
  android:minHeight="?attr/actionBarSize"
58
- android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
58
+ android:theme="?attr/actionBarTheme"
59
59
  app:layout_constraintLeft_toLeftOf="parent"
60
60
  app:layout_constraintRight_toRightOf="parent"
61
61
  app:layout_constraintTop_toTopOf="parent" />
@@ -83,4 +83,30 @@
83
83
  res/menu/menu_main.xmlやres/value/strings.xmlも若干触りましたが、大勢に影響あるものとは思えないので割愛。
84
84
 
85
85
  上記のコードを、デバイスをNexus 5Xにしたエミュレーターで動作させた結果が下図です。全角13文字まで表示されています。
86
- ![](b541eec3f808dc6ecdca522e8c9ebcc7.png)
86
+ ![](b541eec3f808dc6ecdca522e8c9ebcc7.png)
87
+
88
+ ---
89
+
90
+ (参考追記)
91
+ activity_main.xml(LinearLayout版)
92
+ ```XML
93
+ <?xml version="1.0" encoding="utf-8"?>
94
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
95
+ xmlns:tools="http://schemas.android.com/tools"
96
+ android:layout_width="match_parent"
97
+ android:layout_height="match_parent"
98
+ android:orientation="vertical"
99
+ tools:context=".MainActivity">
100
+
101
+ <android.support.v7.widget.Toolbar
102
+ android:id="@+id/toolbar"
103
+ android:layout_width="match_parent"
104
+ android:layout_height="wrap_content"
105
+ android:background="?attr/colorPrimary"
106
+ android:minHeight="?attr/actionBarSize"
107
+ android:theme="?attr/actionBarTheme" />
108
+
109
+ </LinearLayout>
110
+ ```
111
+
112
+ これでも見た目上の結果は全く同じでした。

1

コード詳細を掲載

2017/10/22 05:48

投稿

keicha_hrs
keicha_hrs

スコア6768

answer CHANGED
@@ -2,4 +2,85 @@
2
2
 
3
3
  [アプリバーのセットアップ](https://developer.android.com/training/appbar/setting-up.html)
4
4
 
5
- (表示できる文字数が少ないのは、恐らくToolbarの幅がwrap_contentとしてレイアウトされているためだろうと思いますが・・・)
5
+ (表示できる文字数が少ないのは、恐らくToolbarの幅がwrap_contentとしてレイアウトされているためだろうと思いますが・・・)
6
+
7
+ ---
8
+
9
+ 何が起きているのかよくわからないので、「こうしたらこうなった」というコードだけ載せておきます。
10
+
11
+ MainActivity.java
12
+ ```Java
13
+ public class MainActivity extends AppCompatActivity {
14
+
15
+ @Override
16
+ protected void onCreate(Bundle savedInstanceState) {
17
+ super.onCreate(savedInstanceState);
18
+ setContentView(R.layout.activity_main);
19
+
20
+ String title = "あいうえおかきくけこさしすせそ";
21
+
22
+ Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
23
+ toolbar.setTitle(title);
24
+ setSupportActionBar(toolbar);
25
+ if (getSupportActionBar() != null) {
26
+ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
27
+ }
28
+ }
29
+
30
+ @Override
31
+ public boolean onCreateOptionsMenu(Menu menu) {
32
+ getMenuInflater().inflate(R.menu.menu_main, menu);
33
+ return true;
34
+ }
35
+ }
36
+
37
+ ```
38
+ activity_main.xml
39
+ ```XML
40
+ <?xml version="1.0" encoding="utf-8"?>
41
+ <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
42
+ xmlns:app="http://schemas.android.com/apk/res-auto"
43
+ xmlns:tools="http://schemas.android.com/tools"
44
+ android:layout_width="match_parent"
45
+ android:layout_height="match_parent"
46
+ android:orientation="vertical"
47
+ tools:context=".MainActivity">
48
+
49
+ <android.support.v7.widget.Toolbar
50
+ android:id="@+id/toolbar"
51
+ android:layout_width="0dp"
52
+ android:layout_height="wrap_content"
53
+ android:layout_marginLeft="0dp"
54
+ android:layout_marginRight="0dp"
55
+ android:layout_marginTop="0dp"
56
+ android:background="?attr/colorPrimary"
57
+ android:minHeight="?attr/actionBarSize"
58
+ android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
59
+ app:layout_constraintLeft_toLeftOf="parent"
60
+ app:layout_constraintRight_toRightOf="parent"
61
+ app:layout_constraintTop_toTopOf="parent" />
62
+
63
+ </android.support.constraint.ConstraintLayout>
64
+ ```
65
+
66
+ res/values/styles.xml
67
+ ```XML
68
+ <resources>
69
+
70
+ <!-- Base application theme. -->
71
+ <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
72
+ <!-- Customize your theme here. -->
73
+ <item name="colorPrimary">@color/colorPrimary</item>
74
+ <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
75
+ <item name="colorAccent">@color/colorAccent</item>
76
+ <item name="android:textColorPrimary">#FFFFFF</item>
77
+ <item name="android:textColor">#000000</item>
78
+ </style>
79
+
80
+ </resources>
81
+ ```
82
+
83
+ res/menu/menu_main.xmlやres/value/strings.xmlも若干触りましたが、大勢に影響あるものとは思えないので割愛。
84
+
85
+ 上記のコードを、デバイスをNexus 5Xにしたエミュレーターで動作させた結果が下図です。全角13文字まで表示されています。
86
+ ![](b541eec3f808dc6ecdca522e8c9ebcc7.png)