タイトルの通りですが、
Activity.setTitle()で指定したタイトルは長すぎると
「あいうえおかきくけこ...」になってしまいます。
「長すぎる場合はフォントを小さくする」という処理を入れたいのですが、
まず長すぎるかどうか判別する方法はあるでしょうか?
◆実行環境
Android 7.1.1
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
「長すぎるかどうか判別する方法」はわかりませんが、「長すぎる場合はフォントを小さくする」ことならできそうです。
Activityのタイトルバーの代わりにサポートライブラリーが提供するToolbarを使用、Toolbarの子要素としてTextViewを配置、このTextViewにAPI 26で導入されたAutosizing TextViewsを適用することで実現できます。
res/styles.xml(標準のタイトルバーをNoActionBarに書き換える)
XML
1<resources> 2 3 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 4 <!-- Customize your theme here. --> 5 <item name="colorPrimary">@color/colorPrimary</item> 6 <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 7 <item name="colorAccent">@color/colorAccent</item> 8 </style> 9 10</resources>
activity_main.xmlの例
XML
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <android.support.v7.widget.Toolbar 10 android:id="@+id/toolbar" 11 android:layout_width="0dp" 12 android:layout_height="?attr/actionBarSize" 13 android:layout_marginEnd="0dp" 14 android:layout_marginStart="0dp" 15 android:layout_marginTop="0dp" 16 android:background="?attr/colorPrimary" 17 android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 18 app:layout_constraintLeft_toLeftOf="parent" 19 app:layout_constraintRight_toRightOf="parent" 20 app:layout_constraintTop_toTopOf="parent"> 21 22 <TextView 23 android:id="@+id/toolbar_title" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" 26 android:gravity="center_vertical" 27 android:maxLines="1" 28 android:textColor="@android:color/white" 29 android:textSize="20sp" 30 app:autoSizeMinTextSize="1sp" 31 app:autoSizeStepGranularity="1px" 32 app:autoSizeTextType="uniform" /> 33 34 </android.support.v7.widget.Toolbar> 35 36</android.support.constraint.ConstraintLayout> 37
MainActivity.javaの例
Java
1public class MainActivity extends AppCompatActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 8 Toolbar toolbar = findViewById(R.id.toolbar); 9 setSupportActionBar(toolbar); 10 11 TextView toolbarTitle = findViewById(R.id.toolbar_title); 12 toolbarTitle.setText("あいうえお"); 13 //toolbarTitle.setText("あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもらりるれろ"); 14 } 15}
なお、Autosizing TextViewsを用いるためにはcompileSdkVersionを26以上にする必要があり、さらにminSdkVersionに設定できる下限が14となるため、Android 4.0未満の端末では動かせません。
投稿2017/12/22 06:58
編集2017/12/22 07:00総合スコア6768
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/22 08:38
2017/12/22 09:47