我想在使用片段的配置改变时更改片段的布局..但是在活动布局中重叠片段,所以帮助我如何解决这个...... 和这个类文件.........

public class Self_funded_1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    Self_fragment ls_fragment = new Self_fragment();
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.self_funded_category);
    Configuration config = getResources().getConfiguration();

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        /**
         * Landscape mode of the device
         */

        Log.i("LM", "LM");
         ls_fragment = new Self_fragment();
        fragmentTransaction.replace(android.R.id.content, ls_fragment);

    } else {
        /**
         * Portrait mode of the device
         */
        Log.i("LM", "PM");
        PM_self_funded pm_fragment = new PM_self_funded();

        fragmentTransaction.replace(android.R.id.content, pm_fragment);
        fragmentTransaction.remove(ls_fragment);
        fragmentTransaction.hide(ls_fragment);
    }
    fragmentTransaction.commit();
}
.

这是活动的XML文件..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<fragment
    android:id="@+id/lm_fragment"
    android:name="info.com.forms.Self_fragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

<fragment
    android:id="@+id/pm_fragment"
    android:name="info.com.forms.PM_self_funded"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />
.

这是快照拍摄........

有帮助吗?

解决方案

您也在活动中声明XML中的片段和编程方式。您需要只选择一个选项。

例如,为肖像创建两个不同的布局文件,一个用于横向,然后在那里声明片段,然后您无需在活动中执行任何特殊。

layout/my-layout
layout-land/my-layout

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
}
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top