以编程方式设置presentationDisplay Orientation

时间:2018-01-22 07:22:30

标签: android

我正在处理的应用将有2个显示器。使用DisplayManager我将演示文稿转换为第二个屏幕。

我想找到一种方法来将我的表示类的方向设置为横向,将活动类设置为纵向。代码轰鸣声调用onCreate of activity来呈现演示文稿。

@Override
protected void onCreate(Bundle savedInstanceState) {

    // Get the display manager service.
    mDisplayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);
    Display[] presentationDisplays = mDisplayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
    if (presentationDisplays.length > 0) {

        mProductPresentation = new ProductPresentation(this, presentationDisplays[0], product);
        mProductPresentation.show();

        Log.d(TAG, " on display #" + presentationDisplays[0].getDisplayId() + ".");
    }
}

代码工作正常,显示器正确呈现。我只是想知道是否可以在不改变活动方向的情况下翻转表示类的方向

1 个答案:

答案 0 :(得分:0)

我最终只是抓住布局并旋转它。见下面的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Be sure to call the super class.
    super.onCreate(savedInstanceState);

    //Assign the correct layout
    mViewLayout = getLayoutInflater().inflate(R.layout.presentation_product_details, null);
    setContentView(mViewLayout);

    //We need to wait till the view is created so we can flip it and set the width & height dynamically
    mViewLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener());
}

private class OnGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener{

    @Override
    public void onGlobalLayout() {
        //height is ready
        mViewLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);

        int width = mViewLayout.getWidth();
        int height = mViewLayout.getHeight();

        mViewLayout.setTranslationX((width - height) / 2);
        mViewLayout.setTranslationY((height - width) / 2);
        mViewLayout.setRotation(90.0f);

        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams( height, width);

        // Inflate the layout.
        setContentView(mViewLayout, lp);

        findViews();
    }
}