最初,我的MainActivity中有两个按钮。 Button1对应于显示第一个片段,而button2对应于显示第二个片段。
问题在于,当用户单击button2时,第二个片段已加载,但第一个片段仍然可见。我怎样才能解决这个问题?
这是button1和button2的代码。
if(number == 1) {
//button 1 pressed
BlankFragment fragment1 = new BlankFragment();
Fragment fragment = new FragmentTwo();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.placeFragment, fragment1);
fragmentTransaction.commit();
}
//button2 is pressed
if(number==2) {
FragmentTwo fragment2 = new FragmentTwo();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.placeFragment, fragment2);
fragmentTransaction.commit();
}
分割一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:orientation="vertical"
tools:context=".BlankFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="8">
<ImageView
android:id="@+id/imagess"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_weight="5"
android:scaleType="fitXY" />
<Button
android:id="@+id/button"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="123dp"
android:layout_marginStart="128dp"
android:text="Button" />
</LinearLayout>
</FrameLayout>
分割两个xml文件
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".FragmentTwo">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/itemText"
android:backgroundTint="@color/colorPrimary"
android:paddingTop="6dp"
android:paddingBottom="12dp"
android:layout_weight="2.5"
android:hint="@string/enter_a_item"/>
<Button
android:layout_width="wrap_content"
android:layout_weight="1.5"
android:id="@+id/enterButton"
android:gravity="center"
android:text="Enter"
android:onClick="onEnter"
android:background="@drawable/round"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview"
/>
</LinearLayout>
</FrameLayout>
我的活动片段。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main2Activity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/placeFragment"
android:name="com.example.akhilbatchu.makearecipe.BlankFragment"
/>
</RelativeLayout>
请帮助我。
答案 0 :(得分:1)
也许您在代码中的某处使用add
而不是replace
或
您应将Activity片段xml中的fragment
替换为一些空容器(例如FrameLayout):
替换为:
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/placeFragment"
android:name="com.example.akhilbatchu.makearecipe.BlankFragment"/>
附带:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/placeFragment"/>
答案 1 :(得分:1)
您需要像这样为第二个片段背景着色。还要添加可点击属性
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#FFFFFF"
android:clickable="true"
android:focusable="true"
tools:context=".FragmentTwo">