看着视频学习的,Fragment:3.Fragment使用方法_哔哩哔哩_bilibili
程序的运行效果是,手机页面有2个fragment,每个fragment 有一个text view,一个按钮,按一下显示,'fine,and you?',各自独立。
在android studio 下新建一个工程,类型是 Empty View Activity,本身就有一个Activity。就有文件MainActivity.java 或者kt,还有一个layout 文件,activity_main.xml。新建一个fragment,操作如下图:

可以看到左边有4个文件:程序文件 BlankFragment.kt,MainActivity.kt (Java版本是Java),布局文件activity.xml,fragment_blank.xml,分别对4个文件修改,先简化,再添加点点代码。然后就测试成功了。
最后文件如下:
activity.xml
- "1.0" encoding="utf-8"?>
- <LinearLayout 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"
- android:orientation="vertical"
- tools:context=".MainActivity">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:text="Hello World!" />
- <fragment android:name="com.liwensoft.hellofragment.BlankFragment"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="4"
- android:id="@+id/fragment1"
- />
- <fragment android:name="com.liwensoft.hellofragment.BlankFragment"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="4"
- android:id="@+id/fragment2"
- />
-
-
-
- LinearLayout>
fragment_blank.xml
- "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="vertical">
-
- <TextView
- android:layout_width="match_parent"
- android:layout_height="40dp"
- android:text="@string/hello_blank_fragment"
- android:id="@+id/tv1"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="40dp"
- android:id="@+id/button"
- android:text="how are you"/>
-
- LinearLayout>
应用代码文件MainActivity.kt
- package com.liwensoft.hellofragment
-
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
-
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- }
- }
BlankFragment.kt
- package com.liwensoft.hellofragment
-
- 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
-
-
- class BlankFragment : Fragment() {
-
- private lateinit var tv: TextView
- private lateinit var root:View
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
-
- }
-
- override fun onCreateView(
- inflater: LayoutInflater, container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- // Inflate the layout for this fragment
- root= inflater.inflate(R.layout.fragment_blank, container, false)
- tv=root.findViewById
(R.id.tv1) - val button=root.findViewById
- button.setOnClickListener( ){
- tv.setText("fine, and you?")
- }
-
- return root
- }
-
- }
因为视频是java 语言,首先完成的是java ,其布局文件一样,代码是java的,分别如下:
MainActivity.java
- package com.liwensoft.hellofragmentjava;
-
- 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);
- }
- }
FragmentBlank.java
- package com.liwensoft.hellofragmentjava;
-
- 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;
-
- public class BlankFragment extends Fragment {
-
-
- private View root;
- private TextView textview;
- private Button button;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- if(root==null) {
- root = inflater.inflate(R.layout.fragment_blank, container, false);
- }
- textview=root.findViewById(R.id.tv1);
- button=root.findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- textview.setText("fine,and you?");
- }
- });
- return root;
- }
- }
代码简单易懂,但我开始转了很多地方,列出方便初学者上手。