00-android车载入门

如何选择页面展示的屏幕

  1. Presentation – 指定display的Dialog
1
2
3
4
5
public class Presentation extends Dialog {
public Presentation(Context outerContext, Display display) {
this(outerContext, display, 0);
}
}
  • 本质是dialog

  • 传入display

  • Presentation 的上下文与其宿主 Activity 的上下文相互独立。加载布局、资源时必须使用 Presentation 自身的上下文进行布局填充与资源加载,才能保证加载出适配目标显示器尺寸、屏幕密度的资源素材。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public Presentation(@NonNull Context outerContext, @NonNull Display display, int theme,
@WindowType int type) {
super(createPresentationContext(outerContext, display, theme, type), theme, false);

mDisplay = display;
mDisplayManager = getContext().getSystemService(DisplayManager.class);

final Window w = getWindow();
final WindowManager.LayoutParams attr = w.getAttributes();
w.setAttributes(attr);
w.setGravity(Gravity.FILL);
w.setType(getWindowType(type, display));
setCanceledOnTouchOutside(false);
}

private static Context createPresentationContext(
Context outerContext, Display display, int theme, @WindowType int type) {
if (outerContext == null) {
throw new IllegalArgumentException("outerContext must not be null");
}
if (display == null) {
throw new IllegalArgumentException("display must not be null");
}

Context windowContext = outerContext.createDisplayContext(display)
.createWindowContext(getWindowType(type, display), null /* options */);
if (theme == 0) {
TypedValue outValue = new TypedValue();
windowContext.getTheme().resolveAttribute(
com.android.internal.R.attr.presentationTheme, outValue, true);
theme = outValue.resourceId;
}
return new ContextThemeWrapper(windowContext, theme);
}
  • 当 Presentation 所依附的显示器被移除时,Presentation 会自动关闭(对应 Dialog.cancel())。宿主 Activity 在自身执行暂停 / 恢复生命周期时,需要自行控制 Presentation 内部正在播放的内容,同步执行暂停、恢复操作。
阅读更多