简体   繁体   English

片段布局未膨胀

[英]Fragment layout not being inflated

I have an application where the device switches fragments depending on your orientation. 我有一个应用程序,设备会根据您的方向切换片段。 Each one has a text view and an inflater in the respective java class. 每个类在各自的java类中都有一个文本视图和一个充气器。 The application runs fine, but doesn't inflate the fragments. 该应用程序运行良好,但不会膨胀这些片段。 I am a beginner when it comes to fragments. 关于片段,我是一个初学者。 Here is my code: 这是我的代码:

MainActivity.java 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 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 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 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 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 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>

When you need change the layout at run time , you should not declare the fragment inside the activity's layout file. 当需要在运行时更改布局时,不应在活动的布局文件中声明该片段。 Replace your activity_main.xml with 将您的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>

and change your fragment creation code to 并将您的片段创建代码更改为

    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);

    }

the doc here will help you understand more about fragments 这里的文档将帮助您了解有关片段的更多信息

Do not code anything in MainActivity, As the link given by @jobcrazy do it directly in xml: 不要在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" />

That should solve it! 那应该解决!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM