Android Fragment(一):基本使用

activity_main.xml
<?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:gravity="center"
    android:orientation="horizontal" >
    <!-- 静态加载fragment:把fragment当成一个ui标签使用 -->
    <fragment
        android:id="@+id/first"
        android:name="com.gatsby.fragmentone.FirstFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <fragment
        android:id="@+id/second"
        android:name="com.gatsby.fragmentone.SecondFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>fragment_first.xml
<?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:orientation="vertical" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="FirstFragment"
        android:textSize="64sp" 
        android:gravity="center"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/image1" />
</LinearLayout>fragment_second.xml
<?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:orientation="vertical" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="SecondFragment"
        android:textSize="64sp" 
        android:gravity="center"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/image8" />
</LinearLayout>MaInActivity.java
package com.gatsby.fragmentone;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}FirstFragment.java
package com.gatsby.fragmentone;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        return view;
    }
}SecondFragment.java
package com.gatsby.fragmentone;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment{
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_second, container, false);
        return view;
    }
} 相关推荐
  xfcyhades    2020-11-20  
   Michael    2020-11-03  
   业余架构师    2020-10-09  
   OuNuo0    2020-09-29  
   moses    2020-09-22  
   Angelia    2020-09-11  
   qinxu    2020-09-10  
   刘炳昭    2020-09-10  
   Nostalgiachild    2020-09-07  
   Nostalgiachild    2020-08-17  
   leavesC    2020-08-14  
   一青年    2020-08-13  
   AndroidAiStudy    2020-08-07  
   ydc0    2020-07-30  
   绿豆饼    2020-07-28