【问题标题】:Change Fragment layout on orientation按方向更改片段布局
【发布时间】:2016-10-01 05:39:41
【问题描述】:

我需要一个解决方案! addView() 中的错误我不知道答案.....

我想改变布局,使用之前的视图

我需要帮助!

图表----https://github.com/PhilJay/MPAndroidChart

进程:com.example.tiago.alsrm_android,PID:23337 java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。 在 android.view.ViewGroup.addViewInner(ViewGroup.java:3784) 在 android.view.ViewGroup.addView(ViewGroup.java:3637) 在 android.view.ViewGroup.addView(ViewGroup.java:3582) 在 android.view.ViewGroup.addView(ViewGroup.java:3558) 在 com.example.tiago.alsrm_android.Fragment.EDA_Fragment.onConfigurationChanged(EDA_Fragment.java:174)

公共类 EDA_Fragment 扩展片段 {

private Intent intentService;
private Chart chart = null;
private View fragmentRootContainer;

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

    if(fragmentRootContainer == null) {
        fragmentRootContainer = inflater.inflate(R.layout.activity_exam, container, false);

        if (chart == null)
            chart = new Chart((LineChart) fragmentRootContainer.findViewById(R.id.chart), EDA, 1023f, 1f);


    return fragmentRootContainer;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    LayoutInflater inflater = LayoutInflater.from(getActivity());
    ViewGroup viewGroup = (ViewGroup) getView();
    viewGroup.removeAllViewsInLayout();

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

        View view = inflater.inflate(R.layout.activity_exam, null);

        LineChart lineChart = (LineChart)view.findViewById(R.id.chart);
        lineChart.addView(chart.getChart());


    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

        View view = inflater.inflate(R.layout.activity_exam, null);

        LineChart lineChart = (LineChart)view.findViewById(R.id.chart);
        lineChart.addView(chart.getChart());

    }
}

}

【问题讨论】:

  • 什么是图表和ChartLine?
  • 同时添加logcat......
  • loagcat 添加.......

标签: android android-fragments


【解决方案1】:

--------首先--------

在 AndroidManifest 中添加这个

android:name=".Activity.EDA_Activity"
android:configChanges="orientation"

------秒----------

  in layout PORTRAIT add this -> android:orientation="vertical"

  in layout LANDSCAPE add this ->  android:orientation="horizontal"

--------第三个-----------

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    LayoutInflater inflater = LayoutInflater.from(getActivity());
    LinearLayout linearLayout = (LinearLayout) getView();
    if(linearLayout != null)
        linearLayout.removeAllViewsInLayout();

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

        ViewGroup parentViewGroup = (ViewGroup)chart.getChart().getParent();
        if (parentViewGroup != null)
            parentViewGroup.removeView(chart.getChart());

        fragmentRootContainer = inflater.inflate(R.layout.activity_exam, linearLayout, true);
        LineChart lineChart = (LineChart) fragmentRootContainer.findViewById(R.id.chart);
        lineChart.addView(chart.getChart());
    }
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

        ViewGroup parentViewGroup = (ViewGroup)chart.getChart().getParent();
        if (parentViewGroup != null)
            parentViewGroup.removeView(chart.getChart());

        fragmentRootContainer = inflater.inflate(R.layout.activity_exam, linearLayout, true);
        inflater.inflate(R.layout.activity_exam, linearLayout, false);

        LineChart lineChart = (LineChart) fragmentRootContainer.findViewById(R.id.chart);
        lineChart.addView(chart.getChart());
    }
}

【讨论】: