<h1>相关资料:</h1>
<p>相关视频:</p>
<p><a href="https://www.imooc.com/learn/894">Fragment应用上</a></p>
<p><a href="https://www.imooc.com/learn/904">Fragment应用下</a></p>
<p>相关文章:</p>
<p><a href="https://blog.csdn.net/handsome_926/article/details/50736024?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-1&spm=1001.2101.3001.4242">Fragment 用法总结(一)</a></p>
<h1>1、Fragment应用</h1>
<h2>1.1、Fragment静态应用</h2>
<h3>1.1.1、代码实例</h3>
<p>实现案例效果:两个Fragment构成Activity的布局,一个标题Fragment,一个内容Fragment</p>
<p><strong>TitleFragment</strong></p>
<pre class="blockcode"><code class="language-java">/**
* 创建和使用Fragment的步骤:
* 1、创建子类继承Fragment
* 2、重写onCreateView()方法,该方法主要定义Fragment的布局,以view对象的方式返回Fragment的布局
* 3、将Fragment引入到Activity中
*/
public class TitleFragment extends Fragment {
@Nullable
@Override
@SuppressLint("InflateParams")
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_title, null);
RelativeLayout rl=view.findViewById(R.id.rl_title);
rl.setOnClickListener(v ->
Toast.makeText(getActivity(),"点击了标题栏",Toast.LENGTH_SHORT).show());
return view;
}
}</code></pre>
<p>布局文件:fragment_title</p>
<pre class="blockcode"><code><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/light_blue_2e">
<ImageView
android:id="@+id/iv_back"
android:layout_width="35dp"
android:layout_height="35dp"
android:paddingLeft="15dp"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:src="@drawable/arrow_back_black"
/>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="我是标题"
android:textSize="20sp"/>
</RelativeLayout></code></pre>
<p><strong>ContentFragment</strong></p>
<pre class="blockcode"><code class="language-java">public class ContentFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_content, null);
}
}</code></pre>
<p>布局文件:fragment_content</p>
<pre class="blockcode"><code><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="我是内容"
android:textSize="30sp"/>
</RelativeLayout></code></pre>
<p><strong>Test1FragmentActivity</strong></p>
<pre class="blockcode"><code class="language-java">/**
* @author songzi522
*
* 静态使用Fragment
* 步骤:
* 1、继承Fragment,重写onCreateView()回调方法,设置Fragment的布局
* 2、在Activity中声明Fragment,使用方式和View相同
*
* 实现案例效果:两个Fragment构成Activity的布局,一个标题Fragment,一个内容Fragment
*
*/
public class Test1FragmentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_test1_fragment);
}
}</code></pre>
<p>布局文件:</p>
<p> </p>
<pre class="blockcode"><code><?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=".fragment.Test1FragmentActivity">
<fragment
android:id="@+id/fragment_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:name="com.gs.common3.fragment.TitleFragment"
/>
<fragment
android:id=" |
|