Android多屏显示——Presentation

本文详细介绍了如何在Android4.2及以上版本利用Presentation类创建和管理副屏显示,包括展示、控制和保持副屏常驻的方法,以及权限管理和UI注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Presentation 介绍

        要了解 API 的具体调用,推荐先查看官方的文档:Presentation文档

        Android 从4.2开始支持双屏显示,开发时需 minSdkVersion >= 17 。Android 连接两个屏幕时,自动分配主屏和副屏,主屏显示正常的 Activity 界面,副屏通过创建 Presentation 类来实现。

        通过查看 Presentation 继承关系可知,Presentation 继承自 Dialog,创建的时候需要遵循 Dialog 相关要求。当和 Presentation 相关联的屏幕被移除后,Presentation 也会自动的被移除,所以当 Activity 处于 pause 和 resume 的状态时,Presentation 也需要特别注意当前显示的内容的状态。

二、Presentation 实现

        首先建立一个空工程,然后创建MyPresentation类继承Presentation类,由于我需要在副屏界面创建2个Button,并监听点击事件所以我implements了View.OnClickListener接口。

//MyPresentation.java
package com.example.multidisplaydomo;

import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MyPresentation extends Presentation  implements View.OnClickListener{

    public MyPresentation(Context context, Display display){
        super(context,display);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mypresentation);

        Button mypresentation_button_1 = (Button) findViewById(R.id.mypresentation_button_1);
        Button mypresentation_button_2 = (Button) findViewById(R.id.mypresentation_button_2);

        mypresentation_button_1.setOnClickListener(this);
        mypresentation_button_2.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mypresentation_button_1:
                Toast.makeText(getContext(),"mypresentation_button_1 onClick",Toast.LENGTH_SHORT).show();
                break;
            case R.id.mypresentation_button_2:
                Toast.makeText(getContext(),"mypresentation_button_2 onClick",Toast.LENGTH_SHORT).show();
                break;
           
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值