笔记如下
如图:
-
源码
文件结构目录
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.chen.fregment.MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dip">
</FrameLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:onClick="sound"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="声音"
/>
<Button
android:onClick="display"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="显示"
/>
<Button
android:onClick="storge"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="存储"
/>
</LinearLayout>
</LinearLayout>
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 version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="#44ff0000"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="30sp"
android:text="声音的fragment"
/>
</LinearLayout>
其他的xml与类都一样