enter image description here新的片断没有完全取代旧片段
这是布局后,我点击我的图片按钮,新的片段没有完全取代旧的片段......
这是我的旧片段菜单的java代码
public class CMenu extends Fragment implements View.OnClickListener{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_cmenu,container,false);
}
@Override
public void onViewCreated(View view,@Nullable Bundle savedInstanceState){
super.onViewCreated(view,savedInstanceState);
initView(view);
}
private void initView(View view){
ImageButton prebtn = (ImageButton) view.findViewById(R.id.premium_cake);
prebtn.setOnClickListener(this);
ImageButton homemade = (ImageButton) view.findViewById(R.id.homemade);
homemade.setOnClickListener(this);
ImageButton sc_cake = (ImageButton) view.findViewById(R.id.sc_cake);
sc_cake.setOnClickListener(this);
ImageButton special_food = (ImageButton)
view.findViewById(R.id.special_food);
special_food.setOnClickListener(this);
ImageButton delicious_drink = (ImageButton)
view.findViewById(R.id.delicious_drink);
delicious_drink.setOnClickListener(this);
ImageButton other = (ImageButton) view.findViewById(R.id.other);
other.setOnClickListener(this);
}
@Override
public void onClick(View view){
switch (view.getId()){
case R.id.premium_cake:
getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new
PremiumCake()).addToBackStack(null).commit();
break;
case R.id.homemade:
getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new
HomeMade()).addToBackStack(null).commit();
break;
case R.id.sc_cake:
getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new
SpecialCake()).addToBackStack(null).commit();
break;
case R.id.delicious_drink:
getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new
DeliciousDrink()).addToBackStack(null).commit();
break;
case R.id.special_food:
getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new
SignatureFood()).addToBackStack(null).commit();
break;
case R.id.other:
getFragmentManager().beginTransaction().replace(R.id.MenuFragment,new
Other()).addToBackStack(null).commit();
break;
}
}
this is my new fragment java code
public class Other extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.other, container, false);
}
旧片段XML(我使用此上滚动视图)
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="?android:windowBackground"
android:layout_height="wrap_content"
tools:context=".MenuFragment"
tools:ignore="ContentDescription"
android:id="@+id/MenuFragment">
<ImageButton
android:layout_width="match_parent"
android:id="@+id/sc_cake"
android:background="@drawable/sc_bg"
android:layout_height="120dp"
android:layout_below="@+id/homemade"
android:layout_alignParentStart="true"
/>
<ImageButton
android:layout_width="match_parent"
android:id="@+id/premium_cake"
android:background="@drawable/pc_bg"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="50dp" />
<ImageButton
android:layout_width="match_parent"
android:id="@+id/homemade"
android:background="@drawable/hmc_bg"
android:layout_height="120dp"
android:layout_below="@+id/premium_cake"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="match_parent"
android:id="@+id/delicious_drink"
android:background="@drawable/dd_bg"
android:layout_height="120dp"
android:layout_below="@+id/special_food"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="match_parent"
android:id="@+id/other"
android:background="@drawable/other_bg"
android:layout_height="120dp"
android:layout_below="@+id/delicious_drink"
android:layout_alignParentStart="true"
android:layout_marginBottom="50dp" />
<ImageButton
android:layout_width="match_parent"
android:id="@+id/special_food"
android:background="@drawable/sf_bg"
android:layout_height="120dp"
android:layout_below="@+id/sc_cake"
android:layout_alignParentStart="true" />
</RelativeLayout>
</ScrollView>
新片段的xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<GridLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:background="#2e3192">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<Button
android:id="@+id/cb_big"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="180dp"
android:background="@drawable/coolerbag_big" />
<Button
android:id="@+id/cb_small"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="180dp"
android:background="@drawable/coolerbag_small" />
</LinearLayout>
</GridLayout>
</ScrollView>
请输入你写的代码来代替片段 –
我很抱歉,因为这是我第一次发布在strackoverflow。代码我在我的文章中更新 – JsLaw
您将Fragment本身作为Container,您需要包含片段的布局ID。我会上传下面的代码。 –