Adroid中Fragment的简单使用

下面四个步骤就能创建一个简单的fragment

10年积累的网站制作、成都网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计制作后付款的网站建设流程,更有淮阴免费网站建设让你可以放心的选择与我们合作。

1. 扩展Fragment class

2. 在XML 或 Java中提供显示

3. 覆盖onCreateView方法

4. 在activity中使用Fragment

如下就是简单的显例

创建一个FirstActivityFragment.java文件,扩展Fragment class

package com.example.liang.login;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.VelocityTrackerCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by liang on 2016/7/15.
 */
public class FirstActivityFragment extends Fragment {

    public FirstActivityFragment(){

    }


    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState){
       View v = inflater.inflate(R.layout.fragment_first,
                container, false);


        TextView textView = (TextView)v.findViewById(R.id.showFirstFragmentInfo);
        final Context context = this.getActivity();
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast hello;
                hello = Toast.makeText(context,"hello",Toast.LENGTH_LONG);
                hello.show();
            }
        });
        return v;
    }

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

}

为Fragment创建一个xml fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/showFirstFragmentInfo"
        android:text="this is a firstFragment"/>
</LinearLayout>

并在一个activity view 中使用

<?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"
    android:background="#33FF00">
    <fragment xmlns:android=
        "http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/fragment"
        android:name="com.example.liang.login.FirstActivityFragment"
        tools:layout="@layout/fragment_first"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

本文标题:Adroid中Fragment的简单使用
标题链接:https://www.cdcxhl.com/article34/pihhse.html

成都网站建设公司_创新互联,为您提供网站建设网站改版App开发电子商务小程序开发企业建站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设