采用系统字幕设置
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在 Android TV 上,为用户提供了定义自己的字幕样式的设置。
本指南演示了应用如何获取和应用系统提供的
图片说明样式。
字幕选项位于设置 >系统 >无障碍功能 >图片说明:

获取 CaptioningManager
在 activity 中,您可以使用以下代码从其 Context
获取字幕服务
CaptioningManager
:
CaptioningManager captioningManager = (CaptioningManager)context.getSystemService(Context.CAPTIONING_SERVICE);
处理字幕样式更改
然后,您可以通过实现 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();
}
});
如需获取系统 CaptionStyle
,您可以调用 getUserStyle()
直接:
CaptionStyle systemCaptionStyle = captioningManager.getUserStyle();
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],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();"]]