片段布局没有膨胀

时间:2015-03-23 01:41:32

标签: java android xml android-fragments

我有一个应用程序,设备根据您的方向切换片段。每个都有相应的java类中的文本视图和inflater。应用程序运行正常,但不会使碎片膨胀。谈到片段,我是初学者。这是我的代码:

MainActivity.java

public class MainActivity extends ActionBarActivity {

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

    FragmentManager fragmentManager = getFragmentManager();

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    Configuration configInfo = getResources().getConfiguration();

    if(configInfo.orientation == Configuration.ORIENTATION_LANDSCAPE){

        LandscapeFragment fragmentLandscape = new LandscapeFragment();

        fragmentTransaction.replace(android.R.id.content, fragmentLandscape);

    }else{

        PortraitFragment fragmentPortrait = new PortraitFragment();

        fragmentTransaction.replace(android.R.id.content, fragmentPortrait);

    }

    fragmentTransaction.commit();

    }

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/landscape_fragment" />

    <fragment
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/portrait_fragment" />

</RelativeLayout>

LandscapeFragment.java

public class LandscapeFragment extends Fragment{

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

    System.out.println("Landscape Fragment Created");

    View v =  inflater.inflate(R.layout.landscape_fragment, container, false);
    return v;

    }
}

landscape_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/landscape_text_view"
        android:textSize="30sp" />

</RelativeLayout>

PortraitFragment.java

public class PortraitFragment extends Fragment{

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

    System.out.println("Portrait Fragment Created");

    View v = inflater.inflate(R.layout.portrait_fragment, container, false);
    return v;

    }
}

portrait_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

    <TextView android:text="@string/portrait_text_view"         android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textSize="30sp"/>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

如果需要在运行时更改布局,则不应在活动的布局文件中声明片段。将您的activity_main.xml替换为

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_content" />

</RelativeLayout>

并将您的片段创建代码更改为

    if(configInfo.orientation == Configuration.ORIENTATION_LANDSCAPE){

        LandscapeFragment fragmentLandscape = new LandscapeFragment();

        fragmentTransaction.replace(R.id.main_content, fragmentLandscape);

    }else{

        PortraitFragment fragmentPortrait = new PortraitFragment();

        fragmentTransaction.replace(R.id.main_content, fragmentPortrait);

    }

文档here将帮助您了解有关片段的更多信息

答案 1 :(得分:0)

不要在MainActivity中编写任何代码, 由@jobcrazy给出的链接直接在xml中执行:

<fragment
    android:name="yourpackage.PortraitFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/landscape_fragment" />

<fragment
   android:name="yourpackage.LandscapeFragment
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/portrait_fragment" />

那应该解决它!