Android(安卓)Fragment
16lz
2021-12-04
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());切换
更多相关文章
- Ndk配置与Demo
- Android:上下拖动切换界面
- Android(安卓)复制到剪贴板
- Android(安卓)图片渐变切换效果
- Android(安卓)官方通知工具类Notification
- android SQLite数据库使用实例
- Android(安卓)- LayoutInflater 的使用
- Android中TextView所带的各类属性的使用
- Android高级工程师每日一面试题精选!(1——15题)持续更新!