Android:Frament详解
Fragment是什么
具备生命周期;依托Activity运行
Fragment运行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Fragemnt1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/tv1"
android:layout_width="200dp"
android:layout_height="200dp"
android:text="@string/hello_blank_fragment" />
<Button
android:layout_width="200dp"
android:layout_height="200dp"
android:text="按钮1"
android:id="@+id/btn1"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragmentlearn1.Fragemnt1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment
android:id="@+id/fragment2"
android:name="com.example.fragmentlearn1.Fragemnt2"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
package com.example.fragmentlearn1;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
/**
* A simple {@link Fragment} subclass.
* Use the {@link Fragemnt1#newInstance} factory method to
* create an instance of this fragment.
*/
public class Fragemnt1 extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public Fragemnt1() {
// Required empty public constructor
}
private View root;
private TextView textView;
private Button button;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Fragemnt1.
*/
// TODO: Rename and change types and number of parameters
public static Fragemnt1 newInstance(String param1, String param2) {
Fragemnt1 fragment = new Fragemnt1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (root == null)
// Inflate the layout for this fragment
root = inflater.inflate(R.layout.fragment_fragemnt1, container, false);
textView = root.findViewById(R.id.tv1);
button = root.findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("啦啦啦啦啦");
}
});
return root;
}
}
package com.example.fragmentlearn1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}