Fragment使用(上)

论坛 期权论坛     
选择匿名的用户   2021-5-28 00:59   0   0
<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&#61;distribute.pc_relevant.none-task-blog-baidujs_title-1&amp;spm&#61;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 {

    &#64;Nullable
    &#64;Override
    &#64;SuppressLint(&#34;InflateParams&#34;)
    public View onCreateView(LayoutInflater inflater, &#64;Nullable ViewGroup container, Bundle savedInstanceState) {
        View view &#61; inflater.inflate(R.layout.fragment_title, null);
        RelativeLayout rl&#61;view.findViewById(R.id.rl_title);
        rl.setOnClickListener(v -&gt;
                Toast.makeText(getActivity(),&#34;点击了标题栏&#34;,Toast.LENGTH_SHORT).show());
        return view;
    }
}</code></pre>
<p>布局文件:fragment_title</p>
<pre class="blockcode"><code>&lt;?xml version&#61;&#34;1.0&#34; encoding&#61;&#34;utf-8&#34;?&gt;
&lt;RelativeLayout
    xmlns:android&#61;&#34;http://schemas.android.com/apk/res/android&#34;
    android:id&#61;&#34;&#64;&#43;id/rl_title&#34;
    android:layout_width&#61;&#34;match_parent&#34;
    android:layout_height&#61;&#34;50dp&#34;
    android:background&#61;&#34;&#64;color/light_blue_2e&#34;&gt;

    &lt;ImageView
        android:id&#61;&#34;&#64;&#43;id/iv_back&#34;
        android:layout_width&#61;&#34;35dp&#34;
        android:layout_height&#61;&#34;35dp&#34;
        android:paddingLeft&#61;&#34;15dp&#34;
        android:layout_centerVertical&#61;&#34;true&#34;
        android:scaleType&#61;&#34;centerCrop&#34;
        android:src&#61;&#34;&#64;drawable/arrow_back_black&#34;
        /&gt;

    &lt;TextView
        android:id&#61;&#34;&#64;&#43;id/tv_title&#34;
        android:layout_width&#61;&#34;wrap_content&#34;
        android:layout_height&#61;&#34;wrap_content&#34;
        android:layout_centerInParent&#61;&#34;true&#34;
        android:text&#61;&#34;我是标题&#34;
        android:textSize&#61;&#34;20sp&#34;/&gt;

&lt;/RelativeLayout&gt;</code></pre>
<p><strong>ContentFragment</strong></p>
<pre class="blockcode"><code class="language-java">public class ContentFragment extends Fragment {

    &#64;Nullable
    &#64;Override
    public View onCreateView(LayoutInflater inflater, &#64;Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_content, null);
    }
}</code></pre>
<p>布局文件:fragment_content</p>
<pre class="blockcode"><code>&lt;?xml version&#61;&#34;1.0&#34; encoding&#61;&#34;utf-8&#34;?&gt;
&lt;RelativeLayout
    xmlns:android&#61;&#34;http://schemas.android.com/apk/res/android&#34;
    android:id&#61;&#34;&#64;&#43;id/rl_content&#34;
    android:layout_width&#61;&#34;match_parent&#34;
    android:layout_height&#61;&#34;match_parent&#34;&gt;

    &lt;TextView
        android:layout_width&#61;&#34;match_parent&#34;
        android:layout_height&#61;&#34;match_parent&#34;
        android:gravity&#61;&#34;center&#34;
        android:text&#61;&#34;我是内容&#34;
        android:textSize&#61;&#34;30sp&#34;/&gt;

&lt;/RelativeLayout&gt;</code></pre>
<p><strong>Test1FragmentActivity</strong></p>
<pre class="blockcode"><code class="language-java">/**
* &#64;author songzi522
*
* 静态使用Fragment
* 步骤:
* 1、继承Fragment,重写onCreateView()回调方法,设置Fragment的布局
* 2、在Activity中声明Fragment,使用方式和View相同
*
* 实现案例效果:两个Fragment构成Activity的布局,一个标题Fragment,一个内容Fragment
*
*/
public class Test1FragmentActivity extends AppCompatActivity {

    &#64;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>&lt;?xml version&#61;&#34;1.0&#34; encoding&#61;&#34;utf-8&#34;?&gt;
&lt;RelativeLayout
    xmlns:android&#61;&#34;http://schemas.android.com/apk/res/android&#34;
    xmlns:app&#61;&#34;http://schemas.android.com/apk/res-auto&#34;
    xmlns:tools&#61;&#34;http://schemas.android.com/tools&#34;
    android:layout_width&#61;&#34;match_parent&#34;
    android:layout_height&#61;&#34;match_parent&#34;
    tools:context&#61;&#34;.fragment.Test1FragmentActivity&#34;&gt;

    &lt;fragment
        android:id&#61;&#34;&#64;&#43;id/fragment_title&#34;
        android:layout_width&#61;&#34;match_parent&#34;
        android:layout_height&#61;&#34;50dp&#34;
        android:name&#61;&#34;com.gs.common3.fragment.TitleFragment&#34;
        /&gt;

    &lt;fragment
        android:id&#61;&#34
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP