Using onConfigurationChanged in a fragment

前端 未结 1 975
暗喜
暗喜 2021-01-01 17:44

I have this code in a fragment

public class TestOne extends Fragment {

    View view = null;

    @Override
    public void onConfigurationChanged(Configura         


        
相关标签:
1条回答
  • 2021-01-01 18:36

    In onCreateView create FrameLayout - this is the container for you fragmenView. Then create your R.layout.testone and add it to frameLayout.

    In onConfigurationChanged clear FrameLayout, create R.layout.testone again and add it to frameLayout.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
    {
        frameLayout = new FrameLayout(getActivity());
        LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.testone, null);
        frameLayout .addView(view);
        return frameLayout; 
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) 
    {
        super.onConfigurationChanged(newConfig);
        frameLayout. removeAllViews();
        LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.testone, null);
        frameLayout .addView(view);
    }
    

    Now all will work as you want!

    0 讨论(0)
提交回复
热议问题