我正在实现一个“fragments-101”程序,当相应的按钮被点击时我会替换一个 fragment 。然而,下面的事情发生了:
为什么会这样?为什么初始 fragment 没有被完全替换? MainActivity 和两个 fragment xml 文件都使用 LinearLayout。我在这里搜索了这个问题,发现 replace() 方法有时会出现错误,所以我尝试使用 add(),但无济于事。 fragment 中没有特殊的代码,就是一个简单的hello-world类型的 fragment 程序。
我在 AVD 和 targetSDK 中使用的是 Android Lollipop 版本,在 Android Studio 1.0.2 中,minSDK 版本对应于 Android 4.0.3。这可能与 Android Lollipop 相关吗?
主 Activity .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>
最佳答案
Fragments
直接从带有 <fragment/>
的 xml 布局中膨胀的标签无法更换。
你应该有一个空的 FrameLayout
在你的里面 xml
布局。你应该添加、删除、替换你所有的 Fragments
以编程方式进入 FrameLayout
.
关于android-fragments - 替换 fragment 不会完全替换前一个 fragment 。为什么这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27632443/