如何正确旋转片段 [英] How to correctly make a fragment rotate

查看:58
本文介绍了如何正确旋转片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为一个应用程序创建一个片段,该片段将在显示之前旋转(以免更改为横向和纵向,因为我希望避免再次创建该活动).

I am trying to create a fragment for an app that will be rotated before shown (so as to avoid changing to landscape and portrait because I wish to avoid the activity being created again).

当显示第一个片段(列表)时,用户选择一个选项,然后将第二个片段显示为纵向. 为此,我在第二个片段的xml文件中添加了

When the first fragment is shown (a list) the user selects an option, and then the second fragment is shown as if it was in portrait. To do this I have added in the xml file of the second fragment:

android:rotation="90"

但是,这似乎无法正常工作,它看起来像是在纵向模式下绘制了屏幕,然后旋转了90度再显示出来,这具有左右两侧都有大空白的效果,并且片段的标题显示不正确.

However this does not seem to work correctly, it looks like the screen was drawn in portrait mode, and then rotated by 90 degrees and then shown, this has the effect of the right and left side having a big blank, and the header of the fragment not showing correctly.

有人可以通过帮助我解决显示问题或向我展示替代方法来帮助我吗?

Could someone help me with this, either by helping me fix the display issue, or showing me an alternative?

推荐答案

我终于找到了答案 使用按钮以编程方式更改屏幕方向

但是由于它是一个片段,所以我不想旋转整个屏幕,代码如下:

but since it is a fragment I wish to rotate not the whole screen the code is like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (mView != null) return mView; //I use this because I use the same fragment , if you use different fragments remove this
    mView = (LinearLayout) inflater.inflate(R.layout.fragment_customer_card_view, container, false);
    int w = container.getWidth();
    int h = container.getHeight();
    mView.setRotation(90);
    mView.setTranslationX((w - h) / 2);
    mView.setTranslationY((h - w) / 2);
    ViewGroup.LayoutParams lp = mView.getLayoutParams();
    lp.height = w;
    lp.width = h;
    mView.requestLayout();
    return mView;
}

希望它对以后的人有帮助

hope it helps someone in the future

这篇关于如何正确旋转片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆