看着视频学习的,Fragment:3.Fragment使用方法_哔哩哔哩_bilibili
在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
<?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"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid: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
<?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"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="40dp"android:text="@string/hello_blank_fragment"android:id="@+id/tv1"/><Buttonandroid:layout_width="match_parent"android:layout_height="40dp"android:id="@+id/button"android:text="how are you"/></LinearLayout>
应用代码文件MainActivity.kt
package com.liwensoft.hellofragmentimport androidx.appcompat.app.AppCompatActivity
import android.os.Bundleclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)}
}
BlankFragment.kt
package com.liwensoft.hellofragmentimport 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.TextViewclass BlankFragment : Fragment() {private lateinit var tv: TextViewprivate lateinit var root:Viewoverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)}override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {// Inflate the layout for this fragmentroot= inflater.inflate(R.layout.fragment_blank, container, false)tv=root.findViewById<TextView>(R.id.tv1)val button=root.findViewById<Button>(R.id.button)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 {@Overrideprotected 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;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentif(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() {@Overridepublic void onClick(View v) {textview.setText("fine,and you?");}});return root;}
}
代码简单易懂,但我开始转了很多地方,列出方便初学者上手。