Android Fragment

fragment的使用
新建FineTuningModeFragment

public class FineTuningModeFragment extends Fragment {
     

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     

        View view = inflater.inflate(R.layout.fine_tuning_mode_layout, container, false);

        return view;
    }
}

新建fine_tuning_mode_layout

<?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">

    <TextView
        android:text="weitiao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

使用

<fragment
    android:id="@+id/fine_tuning_mode_fragment"
    android:name="com.example.mydemo.hydraumaticrisetower.Fragment.RiseTowerModeFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

两个fragment切换
再新建RiseTowerModeFragment

public class RiseTowerModeFragment extends Fragment {
     

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     

        View view = inflater.inflate(R.layout.rise_tower_mode_layout, container, false);

        return view;
    }
}

新建rise_tower_mode_layout.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">

    <TextView
        android:text="shenta"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

使用
1.

<fragment
    android:id="@+id/fine_tuning_mode_fragment"
    android:name="com.example.mydemo.hydraumaticrisetower.Fragment.RiseTowerModeFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
改为
<FrameLayout
    android:id="@+id/framelayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

activity新增

private void replaceFragment(Fragment fragment) {
     
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.framelayout, fragment);
    transaction.commit();
}

使用
replaceFragment(new RiseTowerModeFragment());replaceFragment(new FineTuningModeFragment());
切换

你可能感兴趣的:(Android,fragment)