2018-03-15 Fragment布局(一)

笔记如下



如图:
Video_2018-03-15_143056.gif


  • 源码
    文件结构目录


    2018-03-15 Fragment布局(一)_第1张图片
    2018-03-15_144211.png

activity_main.xml中




    
    

    

       

MainActivity类

package com.chen.fregment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    FragmentManager manager;
    FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //fragment:不需要去清单文件中声明,直接new
        SoundFragment sf = new SoundFragment();

        //拿到fragment的manager对象
        FragmentManager manager = getFragmentManager();

        //事务(防止花屏)
        FragmentTransaction transaction = manager.beginTransaction();

        //表示使用SoundFragment 去替换之前的fragment
        transaction.replace(R.id.container,sf);

        //提交事务
        transaction.commit();

    }

    //声音
    public void sound(View v){
        //在右侧声明一个fragment
        //fragment:不需要去清单文件中声明,直接new
        SoundFragment sf = new SoundFragment();
        
        //拿到fragment的manager对象

        manager = getFragmentManager();

        //事务(防止花屏)
        transaction = manager.beginTransaction();

        //表示使用SoundFragment 去替换之前的fragment
        transaction.replace(R.id.container,sf);

        //提交事务
        transaction.commit();

    }


    //显示
    public void display(View v){

        DisplayFragment df = new DisplayFragment();
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.container,df);
        transaction.commit();
    }

    //存储
    public void storge(View v){

        StorgeFragment df = new StorgeFragment();
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.container,df);
        transaction.commit();

    }


}

SoundFragment类

package com.chen.fregment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by HP on 2018/3/15.
 */

public class SoundFragment extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //先将fragment声明layout文件,然后装换为一个view对象
        return  inflater.inflate(R.layout.soundfragment,null);

    }
}

soundfragment.xml




    



其他的xml与类都一样

你可能感兴趣的:(2018-03-15 Fragment布局(一))