activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hzx.fragment_hello.MainActivity">
<LinearLayout
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="96dp"
android:background="#CCCCCC"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
f1.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">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="CheckBox" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
HelloFragment.java
package com.example.hzx.fragment_hello;
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;
import android.widget.Button;
import android.widget.Toast;
/**
* Created by hzx on 2018/11/10.
*/
public class HelloFragment extends Fragment {
//初始化Fragment,实例化在Fragment中的成员变量
private Button button;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
//给Fragment加载UI的布局
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.f1,null);
button = (Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),"Hello world",1).show();
}
});
return view;
}
@Override
public void onPause() {
super.onPause();
}
}
MainActivity.java
package com.example.hzx.fragment_hello;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
//如何加载Fragment到Activity中
private FragmentManager manager;
private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = getFragmentManager();
transaction = manager.beginTransaction();
HelloFragment helloFragment = new HelloFragment();
transaction.add(R.id.line,helloFragment);
transaction.commit();
}
}
效果
当点击按钮时,会弹出"Hello world"
Fragment是Activity的子布局,也有自己的生命周期。
网友评论