Android FrameLayout不会被Fragments取代

时间:2014-04-13 22:27:59

标签: android android-layout android-fragments

此应用包含activity_main.xmlmainactivit.java和一个Fragment类。 activity_main.xmlFrameLayout内有一个按钮和RelativeLayout。 单击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没有替换。

1 个答案:

答案 0 :(得分:3)

你发布的所有内容看起来都不错,除非你没有覆盖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”开头。