替换片段不会完全替换先前的片段。为什么这样?

时间:2014-12-24 06:20:39

标签: android android-fragments android-ui android-5.0-lollipop

我正在实施一个"片段-101"程序,在哪里我单击其相应的按钮时替换片段。但是,下面的事情发生了:

On launching the app

On clicking "Launch Fragment 1"

On clicking "Launch Fragment 2"

为什么会这样?为什么初始片段没有完全替换? MainActivity和片段xml文件都使用LinearLayout。我在这里搜索了一下这个问题,发现replace()方法有时会出错,所以我尝试使用add(),但无济于事。片段中没有特殊代码,它是一个简单的hello-world-type片段程序。

我在AVD中使用Android Lollipop版本以及在Android Studio 1.0.2中使用与Android 4.0.3相对应的minSDK版本的targetSDK。这可能与Android Lollipop有关吗?

MainActivity.java

package com.example.fragmenttest;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity {

    FragmentManager fm;
    FragmentTransaction ft;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void launchFragment(View v)
    {
        Fragment frag;

        if(v == findViewById(R.id.btnFrag2))
        {
            frag = new FragmentTwo();
        }
        else
        {
            frag = new FragmentOne();
        }

        fm = this.getFragmentManager();
        ft = fm.beginTransaction();
        ft.replace(R.id.myFrag,frag);
        ft.commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

layout_frag1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ffff">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 1."
        android:id="@+id/textView2" />
</LinearLayout>

layout_frag2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 2."
        android:id="@+id/textView3" />
</LinearLayout>

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    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">

    <Button
        android:layout_width="348dp"
        android:layout_height="wrap_content"
        android:text="Launch Fragment 2"
        android:id="@+id/btnFrag2"
        android:onClick="launchFragment"
        android:layout_gravity="center_vertical" />

    <Button
        android:layout_width="355dp"
        android:layout_height="wrap_content"
        android:text="Launch Fragment 1"
        android:id="@+id/btnFrag1"
        android:onClick="launchFragment"/>

    <fragment
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:name="com.example.fragmenttest.FragmentTwo"
        android:id="@+id/myFrag"
        tools:layout="@layout/layout_frag2" />

</LinearLayout>

1 个答案:

答案 0 :(得分:6)

无法替换直接从带有Fragments标记的xml布局中膨胀的

<fragment/>

FrameLayout布局中应该有一个空的xml。您应该以编程方式添加,删除,将所有Fragments替换为FrameLayout