UI のカスタマイズ

カスタムタブの利点の一つは、アプリにシームレスに統合できることです。カスタムタブ ガイドのこのパートでは、カスタムタブの外観と動作をアプリに合わせて変更する方法を学習します。

ツールバーの色を設定する

カスタムライトカラーパターンを使用したカスタムタブ
ライトモード
カスタム ダークカラーパターンを使用したカスタムタブ
ダークモード

まず、カスタムタブのアドレスバーをカスタマイズして、アプリのテーマと一致させます。次のスニペットは、setDefaultColorSchemeParams() を呼び出して、デフォルトのツールバーの色を変更します。アプリがダーク カラーパターンもサポートしている場合は、.setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, …) で設定します。

// get the current toolbar background color (this might work differently in your app) @ColorInt int colorPrimaryLight = ContextCompat.getColor(MainActivity.this, R.color.md_theme_light_primary); @ColorInt int colorPrimaryDark = ContextCompat.getColor(MainActivity.this, R.color.md_theme_dark_primary);  CustomTabsIntent intent = new CustomTabsIntent.Builder()         // set the default color scheme         .setDefaultColorSchemeParams(new CustomTabColorSchemeParams.Builder()                 .setToolbarColor(colorPrimaryLight)                 .build())         // set the alternative dark color scheme         .setColorSchemeParams(CustomTabsIntent.COLOR_SCHEME_DARK, new CustomTabColorSchemeParams.Builder()                 .setToolbarColor(colorPrimaryDark)                 .build())         .build(); 

ツールバーの背景色と前景色をカスタマイズできるようになりました。

開始時と終了時のカスタム アニメーションを設定

次に、setStartAnimationsetExitAnimation を使用してカスタムの開始アニメーションと終了アニメーションを定義することで、アプリでカスタムタブの起動(と終了)をよりシームレスにできます。

CustomTabsIntent intent = new CustomTabsIntent.Builder()  .setStartAnimations(MainActivity.this, R.anim.slide_in_right, R.anim.slide_out_left) .setExitAnimations(MainActivity.this, android.R.anim.slide_in_left, android.R.anim.slide_out_right) .build(); 

その他のカスタマイズ: タイトル、AppBar の自動非表示、カスタムの閉じるアイコン

カスタムタブの UI をニーズに合わせて調整するためにできることは他にもあります。

  • スクロール時に URL バーを非表示にして、setUrlBarHidingEnabled()(true) を使用してウェブ コンテンツを閲覧するスペースを広げます。
  • setShowTitle()(true) を使用して、URL ではなくドキュメントのタイトルを表示します。
  • アプリのユーザーフローに合うように閉じるボタンをカスタマイズします(デフォルトの X アイコンではなく、戻る矢印を表示するなど)。setCloseButtonIcon()(myCustomCloseIcon)

これらはすべてオプションですが、アプリのカスタムタブのエクスペリエンスを向上させることができます。

Bitmap myCustomCloseIcon = getDrawable(R.drawable.ic_baseline_arrow_back_24)); CustomTabsIntent intent = new CustomTabsIntent.Builder()      .setUrlBarHidingEnabled(true)   .setShowTitle(true)   .setCloseButtonIcon(toBitmap(myCustomCloseIcon))   .build(); 

カスタム リファラを設定する

カスタムタブを起動するときに、アプリを参照元として設定できます。これにより、トラフィックの発生元をウェブサイトに知らせることができます。

CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build() customTabsIntent.intent.putExtra(Intent.EXTRA_REFERRER,        Uri.parse("android-app://" + context.getPackageName())); 

次は、カスタムタブにカスタム アクションを追加する方法について説明します。