Android fragments Android片段复制:Can';t替换静态添加的片段
嗨,我是Android的新手。我尝试替换静态添加到布局文件中的片段。但是,如果我在xml布局中实例化了片段,那么在运行时用另一个片段替换它之后,内容将保持可见 这是我的活动代码:Android fragments Android片段复制:Can';t替换静态添加的片段,android-fragments,Android Fragments,嗨,我是Android的新手。我尝试替换静态添加到布局文件中的片段。但是,如果我在xml布局中实例化了片段,那么在运行时用另一个片段替换它之后,内容将保持可见 这是我的活动代码: import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
public class RetailerActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retailer);
}
public void selectFrag(View view) {
Fragment fr;
if (view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
} else if (view == findViewById(R.id.button3)) {
fr = new FragmentThree();
} else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.commit();
}
}
以下是我的活动XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.1" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.2" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.3" />
<LinearLayout
android:orientation="vertical"
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<fragment
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00ffff">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="This is fragment No.1"
android:textStyle="bold" />
</LinearLayout>
}
两个xml文件的碎片:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00ffff">
<TextView
android:id="@+id/textfrag2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is fragment No.2"
android:textStyle="bold" />
</LinearLayout>
}
提前感谢。我也研究了这个问题,但似乎找不到一个解决方案来静态地在XML文件中放置片段。在ActivityXML文件中,必须用FrameLayout替换片段部分(id可以保持不变,只需将“Fragment”替换为“FrameLayout”);使您的selectFrag方法与FragmentTransaction一起替换片段而不重叠的唯一方法
(如果您想在活动开始时显示一个默认的片段,复制活动的OnCuto方法中的碎片事务代码,并用一个片段交换空白FrAMELayOutlook)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00ffff">
<TextView
android:id="@+id/textfrag2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is fragment No.2"
android:textStyle="bold" />
</LinearLayout>
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTwo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_two, container, false);
}