无法在第二个屏幕上制作全屏Android演示文稿

时间:2014-12-16 08:48:35

标签: java android layout fullscreen commonsware-cwac

我的问题是关于使用设备辅助屏幕的Android工具,即android.app.Presentation

我正在使用此包装器:https://github.com/commonsguy/cwac-presentation

不幸的是我无法强制第二个屏幕布局处于全屏模式:虽然它的宽度与屏幕完全匹配,但它的高度始终限制在屏幕中心的条纹,约为屏幕高度的1/4

相应地,它的所有内容都显示在这个狭窄的边界内,而屏幕的其余部分则保持黑色和无用

对于那些对此API不了解的人,
android.app.Presentation是一种特殊类型的Dialog,显示在设备的辅助屏幕上,
我上面提到的包装器基于PresentationFragment,它扩展了DialogFragment。用户创建一个扩展PresentationFragment的类,并在其被覆盖的onCreateView()中创建他希望在第二个屏幕上显示的布局。例如:

    public class CustomPresentationFragment extends PresentationFragment {

        public static CustomPresentationFragment newInstance(Context ctxt,
                                                             Display display) {

        CustomPresentationFragment frag=new CustomPresentationFragment();

        frag.setDisplay(ctxt, display);

        //we may prepare bundle to pass, if necessary
        //Bundle b=new Bundle();
        //b.putString("key1", value1);
        //b.putString("key2", value2);
        //frag.setArguments(b); 

        return(frag);
        }



      @Override
      public View onCreateView(LayoutInflater inflater,
                               ViewGroup container,
                               Bundle savedInstanceState) {

      View myView = new View(getContext());       

      //here we create layout that we want to be displayed on second screen, and return it

      return(myView);

      }


}

这是PresentationFragment的实施。如我们所见,它包含Presentation变量:

package com.commonsware.cwac.preso;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;

public abstract class PresentationFragment extends DialogFragment
 {
   private Display display = null;
   private Presentation preso = null;

   public Dialog onCreateDialog(Bundle savedInstanceState)
   {
     if (this.preso == null) {
       return super.onCreateDialog(savedInstanceState); 

       //instead of simply returning super value, I have tried the following here,
       //with no success:
       //
       //Dialog dlg = super.onCreateDialog(savedInstanceState);
       //dlg.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
       //dlg.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
       //dlg.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
       //return dlg;

    }

     return this.preso;
   }

   public void setDisplay(Context ctxt, Display display) {
     if (display == null) {
       this.preso = null;
     }
     else {
       this.preso = new Presentation(ctxt, display, getTheme());

       //since Presentation is Dialog, I have tried the same here as well,
       //no success:

       //this.preso.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
       //this.preso.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
       //this.preso.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);


     }

     this.display = display;
   }

   public Display getDisplay() {
     return this.display;
   }

   protected Context getContext() {
     if (this.preso != null) {
       return this.preso.getContext();
     }

     return getActivity();
   }
}

EDIT。 这是我的一个PresentationFragments中的onCreateView()

@Override
      public View onCreateView(LayoutInflater inflater,
                               ViewGroup container,
                               Bundle savedInstanceState) {

      String uri = "somefilepath";
      File imgFile = new  File(uri);
      Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

      ImageView view = new ImageView(getContext());
      view.setImageBitmap(myBitmap);
      view.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

      RelativeLayout pRelative = new RelativeLayout(getContext());   
      pRelative.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
      pRelative.setGravity(Gravity.CENTER);

      pRelative.addView(view);

      return pRelative;       

      }

1 个答案:

答案 0 :(得分:1)

首先,您RelativeLayout的父级可能不是另一个RelativeLayout,因此您不应该尝试将RelativeLayout的{​​{1}}设置为是LayoutParams的实例。

其次,将其重写为XML布局并对其进行充气。你的RelativeLayout.LayoutParams有更好的工作机会,因为它会在通货膨胀时知道它的父母。 RelativeLayout是一个挑剔的野兽。另一个选择是从RelativeLayout切换到可以进行居中的其他内容,例如RelativeLayout