簡體   English   中英

片段布局未膨脹

[英]Fragment layout not being inflated

我有一個應用程序,設備會根據您的方向切換片段。 每個類在各自的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

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

當需要在運行時更改布局時,不應在活動的布局文件中聲明該片段。 將您的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);

    }

這里的文檔將幫助您了解有關片段的更多信息

不要在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" />

那應該解決!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM