Fragment 学习

  看了都会的 

 

 

 

activity_main.xml

<RelativeLayout 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="com.example.testfragment.MainActivity"
    tools:ignore="MergeRootFrame" >
   
    <fragment
        android:name="com.example.fragment.MyTitleFragment"
        android:id="@+id/fragment_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
   
    <fragment
        android:name="com.example.fragment.BottomFragment"
        android:id="@+id/fragment_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

 

MainActivity.java

package com.example.testfragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);
  setContentView(contentView);

  // if (savedInstanceState == null) {
  // getFragmentManager().beginTransaction()
  // .add(R.id.container, new PlaceholderFragment()).commit();
  // }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {

  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }

 /**
  * A placeholder fragment containing a simple view.
  */
 public static class PlaceholderFragment extends Fragment {

  public PlaceholderFragment() {
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.fragment_main, null);
   return rootView;
  }
 }

 

mytitle_layout.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="horizontal"
    android:background="#000000">
   
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="第一个位置"
        android:textColor="#ffffff"
        android:gravity="center"
        />
     <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="第二个位置"
        android:textColor="#ffffff"
        android:gravity="center"
        />
      <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="第三个位置"
        android:textColor="#ffffff"
        android:gravity="center"
        />

</LinearLayout>

 

 

 MyTitleFragment.java

package com.example.fragment;

import com.example.testfragment.R;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyTitleFragment extends Fragment{
 
 @Override
 public void onAttach(Activity activity) {
  super.onAttach(activity);
  sys("onAttach()");
 }
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  sys("onCreate()");
 }
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  sys("onCreateView()");
  
  View view = inflater.inflate(R.layout.mytitle_layout, null);
  
  return view;
 }
 
 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  sys("onActivityCreated()");
 }
 
 @Override
 public void onStart() {
  super.onStart();
  sys("onStart()");
 }
 
 @Override
 public void onResume() {
  super.onResume();
  sys("onResume()");
 }
 
 @Override
 public void onPause() {
  super.onPause();
  sys("onPause()");
 }
 
 @Override
 public void onStop() {
  super.onStop();
  sys("onStop()");
 }
 
 @Override
 public void onDestroyView() {
  super.onDestroyView();
  sys("onDestroyView()");
 }
 
 @Override
 public void onDestroy() {
  super.onDestroy();
  sys("onDestroy()");
 }
 
 @Override
 public void onDetach() {
  super.onDetach();
  sys("onDetach()");
 }
 
 
 private void sys(String s){
  System.out.println(s);
 }

}

 

bottom_layout.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="horizontal"
    android:background="#000000">
   
   
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Tab_1"
        android:textColor="#ffffff"
        android:gravity="center"
        />
   
     <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Tab_2"
        android:textColor="#ffffff"
        android:gravity="center"
        />
    
      <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Tab_3"
        android:textColor="#ffffff"
        android:gravity="center"
        />

</LinearLayout>

 

BottomFragment.java

package com.example.fragment;

import com.example.testfragment.R;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class BottomFragment extends Fragment{
 
 @Override
 public void onAttach(Activity activity) {
  super.onAttach(activity);
 }
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
 }
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.bottom_layout, container, false);
  return view;
 }
 
 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
 }
 
 @Override
 public void onStart() {
  super.onStart();
 }
 
 @Override
 public void onResume() {
  super.onResume();
 }
 
 @Override
 public void onStop() {
  super.onStop();
 }
 
 @Override
 public void onDestroyView() {
  super.onDestroyView();
 }
 
 @Override
 public void onDestroy() {
  super.onDestroy();
 }
 
 @Override
 public void onDetach() {
  super.onDetach();
 }

}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值