簡體   English   中英

Android FrameLayout不會被Fragments取代

[英]Android FrameLayout is not replace by Fragments

這個應用程序包含activity_main.xmlmainactivit.java和一個Fragment類。 activity_main.xmlRelativeLayout有一個Button和FrameLayout 單擊Button時, FrameLayout應該被片段替換,但不會被替換。

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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="loadd"
            android:text="Fragment No.2" />

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

</RelativeLayout>

mainactivity.java

public class MainActivity extends FragmentActivity {

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

    public void loadd(View v) {
        try {
            Fragment fragment = new fr();
            FragmentManager frgManager = getSupportFragmentManager();
            FragmentTransaction trans = frgManager.beginTransaction();
            trans.replace(R.id.maincontainer, fragment);
            trans.commit();
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, "error " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

}

片段類

public class fr extends Fragment{
    public View OnCreateView(LayoutInflater inf,ViewGroup parent,Bundle save){
        return inf.inflate(R.layout.send,parent, false);
        }
}

send.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textmsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 3" />

</LinearLayout>

沒有編譯時或運行時錯誤,但是當我單擊該按鈕時the FrameLayout不會被Fragment替換。

您發布的所有內容看起來都不錯,除非您沒有覆蓋Fragment.onCreateView ,而是您犯了一個錯字。

你有什么

public View OnCreateView(LayoutInflater inf, ViewGroup parent, Bundle save) {
    return inf.inflate(R.layout.test, parent, false);
}

你應該擁有什么

@Override
public View onCreateView(LayoutInflater inf, ViewGroup parent, Bundle save) {
    return inf.inflate(R.layout.test, parent, false);
}

密切注意方法的名稱。 它應該以小寫“o”開頭。

暫無
暫無

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

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