当Android碎片中的方向发生变化时,布局会发生变化



请提出解决方案。

当我旋转我的片段时,它应该变为横向模式并显示另一个布局。但屏幕并没有旋转到横向。

我的代码破解:

 <activity
        android:name=".activites.MainActivity"
        android:launchMode="singleInstance"
        android:configChanges="keyboardHidden|screenLayout|screenSize|orientation"
        />

这是被称为仪表板的主要布局,现在它处于纵向模式:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View  view=View.inflate(getContext(), R.frag_dashboard,null);
     changeview= (ShimmerTextView)view.findViewById(R.id.changeview); 
     return view;
}

当我旋转屏幕时,这个片段变为横向模式并设置另一个布局,而praye_times是新的布局。

   @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(getContext(), "landscape", Toast.LENGTH_SHORT).show();
        view=View.inflate(getContext(), R.layout.prayer_times,null);
    }
}

并且我为praye_times 创建layout_land

如果您的片段在方向改变时没有重新加载的问题,您可以简单地重新加载。

layoutlayout-land文件夹中添加两个同名布局。

这将在加载时显示正确的定向布局,以便在设备旋转时更改布局在片段本身内部的onConfigarationChanged方法中添加以下内容。

@Override
public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE || newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        try {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (Build.VERSION.SDK_INT >= 26) {
             ft.setReorderingAllowed(false);
            }
            ft.detach(this).attach(this).commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

如果在旋转屏幕时调用onCreateView函数,则可以在其中执行以下操作:

if(this.getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE) {
    ......
} else if(this.getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT) {
    .........
}

迟到了,但这将有助于某些

在V4片段中尝试此操作

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (getFragmentManager() != null) {
            getFragmentManager()
                    .beginTransaction()
                    .detach(this)
                    .attach(this)
                    .commit();
        }
    }

您要做的事情相当复杂。安卓碎片并不意味着要轮换。不过,我也遇到了同样的问题,并找到了解决方案。在我的例子中,我想呈现一个包含不同菜单页面的片段,这些页面将根据方向旋转。

只需创建一个Fragment作为基础,并包含一个简单的LinearLayout(或您想要的任何其他布局类型)。此LinearLayout将作为我们菜单的画布:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llMenuCanvas"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</LinearLayout>

接下来,我们想将基本项片段编码为一个抽象类,它将由所有菜单项片段实现:

public abstract class NavMenuItem extends Fragment {
    static final String TAG = "yourTag"; // Debug tag
    LinearLayout canvas;
    View hView; // we'll keep the reference of both views
    View vView;
    // All we'll need to do is set these up on our fragments
    abstract int getVerticalLayoutResource();
    abstract int getHorizontalLayoutResource();
    abstract void setupUI(); // assigns all UI elements and listeners
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.menu_base, container, false); // sets up the layout for this fragment
        // keeping our references to both layout versions
        hView = inflater.inflate(getHorizontalLayoutResource(), container, false);
        vView = inflater.inflate(getVerticalLayoutResource(), container, false);
        canvas = view.findViewById(R.id.llMenuCanvas); // this is the magic part: Our reference to the menu canvas
        // returning our first view depending on orientation
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
            canvas.addView(hView);
        }else{
            canvas.addView(vView);
        }
        return view;
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupUI(); // here we set up our listeners for the first time
    }
    // Here we update the layout when we rotate the device
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        canvas.removeAllViews();
        // Checking screen orientation
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            canvas.addView(hView);
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            canvas.addView(vView);
        }
        setupUI(); // we always need to rebind our buttons
    }
}

这里是一个菜单项片段的例子,它根据设备的方向旋转。

public class NavMenuMain extends NavMenuItem{
    static final String TAG = "yourTag"; // Debug tag
    // Your layout references, as usual
    ImageButton btnCloseMenu;
    // here we set up the layout resources for this fragment
    @Override
    int getVerticalLayoutResource() { // vertical layout version
        return R.layout.menu_main_port;
    }
    @Override
    int getHorizontalLayoutResource() { // horizontal layout version
        return R.layout.menu_main_land;
    }
    @Override
    void setupUI(){
        // Setup button listeners and layout interaction here
        // REMEMBER: the names of your layout elements must match, both for landscape and portrait layouts. Ex: the "close menu" button must have the same id name in both layout versions
    }
}

希望能有所帮助。

您所需要做的就是在res文件夹中打开一个新的layout-land文件夹,并将具有与片段布局相同名称的xml放在那里,框架就会知道要在方向更改时查找该.xml。查看此处了解详细信息。

默认情况下,/res/layout中的布局同时应用于纵向和横向
例如,如果您有

/res/layout/main.xml

您可以添加一个新的文件夹/res/layout land,将main.xml复制到其中并进行必要的调整

另请参阅http://www.androidpeople.com/android-portrait-amp-landscape-differeent-layouts和http://www.devx.com/wireless/Article/40792/1954了解更多选项。

当您更改方向时,您的碎片会被销毁并重新创建(请参阅此以更好地理解)。因此,在onConfigurationChanged中,您膨胀了新布局,但这是无用的,因为当您的片段重新创建时,onCreateView会被再次调用;换句话说,你的旧布局又膨胀了。所以最好这样做:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View  view;
    if(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        view = View.inflate(getContext(), R.frag_dashboard,null);
        changeview = (ShimmerTextView)view.findViewById(R.id.changeview);
    } else(getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        Toast.makeText(getContext(), "landscape", Toast.LENGTH_SHORT).show();
        view = View.inflate(getContext(), R.layout.prayer_times,null);
    }
    return view;
}

最新更新