Adopter les paramètres système pour les sous-titres
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Sur Android TV, des paramètres sont fournis aux utilisateurs pour définir leur propre style de sous-titres.
Ce guide explique comment une application peut obtenir et appliquer les
style des sous-titres.
Les options de sous-titres se trouvent sous Paramètres > Système > Accessibilité > Légende :

Obtenir le CaptioningManager
À partir d'une activité, vous pouvez obtenir le service de sous-titres à partir de son Context
en utilisant
CaptioningManager
:
CaptioningManager captioningManager = (CaptioningManager)context.getSystemService(Context.CAPTIONING_SERVICE);
Gérer les modifications du style des sous-titres
Vous pouvez ensuite gérer les changements de style des sous-titres en implémentant CaptioningChangeListener
:
if (captioningManager != null) {
// Define a class to store the CaptionStyle details.
CurrentCaptionStyle currentCaptionStyle = new CurrentCaptionStyle;
// Define the listeners.
captioningManager.addCaptioningChangeListener(new CaptioningChangeListener() {
@Override
public void onEnabledChanged(boolean enabled) {
super.onEnabledChanged(enabled);
Log.d(TAG, "onEnabledChanged");
currentCaptionStyle.isEnabled = enabled;
}
@Override
public void onLocaleChanged(@Nullable Locale locale) {
super.onLocaleChanged(locale);
Log.d(TAG, "onLocaleChanged");
currentCaptionStyle.locale = locale;
}
@Override
public void onFontScaleChanged(float fontScale) {
super.onFontScaleChanged(fontScale);
Log.d(TAG, "onFontScaleChanged");
currentCaptionStyle.fontScale = fontScale;
}
@Override
public void onUserStyleChanged(@NonNull CaptionStyle userStyle) {
super.onUserStyleChanged(userStyle);
Log.d(TAG, "onUserStyleChanged");
currentCaptionStyle.hasBackgroundColor = userStyle.hasBackgroundColor();
currentCaptionStyle.backgroundColor = userStyle.backgroundColor;
currentCaptionStyle.backgroundOpcity = userStyle.backgroundColor >>> 24;
currentCaptionStyle.hasForegroundColor = userStyle.hasForegroundColor();
currentCaptionStyle.foregroundColor = userStyle.foregroundColor;
currentCaptionStyle.foregroundOpacity = userStyle.foregroundColor >>> 24;
currentCaptionStyle.hasWindowColor = userStyle.hasWindowColor();
currentCaptionStyle.windowColor = userStyle.windowColor;
currentCaptionStyle.windowOpcity = userStyle.windowColor >>> 24;
currentCaptionStyle.hasEdgeColor = userStyle.hasEdgeColor();
currentCaptionStyle.edgeColor = userStyle.edgeColor;
currentCaptionStyle.hasEdgeType = userStyle.hasEdgeType();
currentCaptionStyle.edgeType = userStyle.edgeType;
currentCaptionStyle.typeFace = userStyle.getTypeface();
}
});
Pour obtenir le système CaptionStyle
, vous pouvez appeler getUserStyle()
directement:
CaptionStyle systemCaptionStyle = captioningManager.getUserStyle();
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/27 (UTC).
[null,null,["Dernière mise à jour le 2025/07/27 (UTC)."],[],[],null,["# Adopt system caption settings\n\nOn Android TV, settings are provided for users to define their own caption style.\nThis guide demonstrates how an app can obtain and apply the system-provided\ncaption style.\n\nThe caption options can be found under **Settings \\\u003e System \\\u003e Accessibility \\\u003e Caption**:\n\nObtain the CaptioningManager\n----------------------------\n\nFrom an activity, you can get the caption service from its `Context` using\n[`CaptioningManager`](https://developer.android.com/reference/android/view/accessibility/CaptioningManager): \n\n CaptioningManager captioningManager = (CaptioningManager)context.getSystemService(Context.CAPTIONING_SERVICE);\n\nHandle caption style changes\n----------------------------\n\nYou can then handle caption style changes by implementing [`CaptioningChangeListener`](/reference/android/view/accessibility/CaptioningManager.CaptioningChangeListener): \n\n if (captioningManager != null) {\n // Define a class to store the CaptionStyle details.\n CurrentCaptionStyle currentCaptionStyle = new CurrentCaptionStyle;\n // Define the listeners.\n captioningManager.addCaptioningChangeListener(new CaptioningChangeListener() {\n\n @Override\n public void onEnabledChanged(boolean enabled) {\n super.onEnabledChanged(enabled);\n Log.d(TAG, \"onEnabledChanged\");\n currentCaptionStyle.isEnabled = enabled;\n }\n\n @Override\n public void onLocaleChanged(@Nullable Locale locale) {\n super.onLocaleChanged(locale);\n Log.d(TAG, \"onLocaleChanged\");\n currentCaptionStyle.locale = locale;\n }\n\n @Override\n public void onFontScaleChanged(float fontScale) {\n super.onFontScaleChanged(fontScale);\n Log.d(TAG, \"onFontScaleChanged\");\n currentCaptionStyle.fontScale = fontScale;\n }\n\n @Override\n public void onUserStyleChanged(@NonNull CaptionStyle userStyle) {\n super.onUserStyleChanged(userStyle);\n Log.d(TAG, \"onUserStyleChanged\");\n currentCaptionStyle.hasBackgroundColor = userStyle.hasBackgroundColor();\n currentCaptionStyle.backgroundColor = userStyle.backgroundColor;\n currentCaptionStyle.backgroundOpcity = userStyle.backgroundColor \u003e\u003e\u003e 24;\n currentCaptionStyle.hasForegroundColor = userStyle.hasForegroundColor();\n currentCaptionStyle.foregroundColor = userStyle.foregroundColor;\n currentCaptionStyle.foregroundOpacity = userStyle.foregroundColor \u003e\u003e\u003e 24;\n currentCaptionStyle.hasWindowColor = userStyle.hasWindowColor();\n currentCaptionStyle.windowColor = userStyle.windowColor;\n currentCaptionStyle.windowOpcity = userStyle.windowColor \u003e\u003e\u003e 24;\n currentCaptionStyle.hasEdgeColor = userStyle.hasEdgeColor();\n currentCaptionStyle.edgeColor = userStyle.edgeColor;\n currentCaptionStyle.hasEdgeType = userStyle.hasEdgeType();\n currentCaptionStyle.edgeType = userStyle.edgeType;\n currentCaptionStyle.typeFace = userStyle.getTypeface();\n }\n\n });\n\nTo obtain the system `CaptionStyle`, you can call `getUserStyle()`\ndirectly: \n\n CaptionStyle systemCaptionStyle = captioningManager.getUserStyle();"]]