Fragment 的基本使用

it2026-02-11  17

fragment1 的布局

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <? 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:background = "#ff00ff"      android:orientation = "vertical" >         < TextView          android:id = "@+id/fr1"          android:layout_width = "fill_parent"          android:layout_height = "wrap_content"          android:gravity = "center_horizontal"          android:text = "fragment1" />    </ LinearLayout >

 

fragment2 的布局

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <? 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:background = "#0000ff"      android:orientation = "vertical" >         < TextView          android:id = "@+id/fr2"          android:layout_width = "match_parent"          android:layout_height = "wrap_content"          android:gravity = "center_horizontal"          android:text = "fragment2"          android:textColor = "#ffffff" />    </ LinearLayout >

activty_main 的布局

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 < LinearLayout 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"      tools:context = ".MainActivity"      android:orientation = "horizontal"       >      < fragment                     android:name = "com.sanya.fragment.Fragment1"          android:layout_width = "fill_parent"          android:layout_weight = "1"          android:layout_height = "match_parent" />         < fragment          android:name = "com.sanya.fragment.Fragment2"          android:layout_width = "fill_parent"          android:layout_weight = "1"          android:layout_height = "match_parent" />    </ LinearLayout >

java  代码

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.sanya.fragment;    import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;    public class Fragment1 extends Fragment {         @Override      public View onCreateView(LayoutInflater inflater, ViewGroup container,              Bundle savedInstanceState) {                                   return  inflater.inflate(R.layout.fragment1, container, false );      } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.sanya.fragment;    import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;    public class Fragment2 extends Fragment {         @Override      public View onCreateView(LayoutInflater inflater, ViewGroup container,              Bundle savedInstanceState) {                                   return  inflater.inflate(R.layout.fragment2, container, false );      } }

 

 

 

 

 

 

 

 

 

来自为知笔记(Wiz)

转载于:https://www.cnblogs.com/feelbest/p/3696202.html

相关资源:数据结构—成绩单生成器
最新回复(0)