Adroid中Fragment的簡單使用

下面四個步驟就能創(chuàng)建一個簡單的fragment

10年積累的網(wǎng)站制作、成都網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計制作后付款的網(wǎng)站建設(shè)流程,更有淮陰免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

1. 擴(kuò)展Fragment class

2. 在XML 或 Java中提供顯示

3. 覆蓋onCreateView方法

4. 在activity中使用Fragment

如下就是簡單的顯例

創(chuàng)建一個FirstActivityFragment.java文件,擴(kuò)展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創(chuàng)建一個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>

本文標(biāo)題:Adroid中Fragment的簡單使用
標(biāo)題鏈接:http://muchs.cn/article34/pihhse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站改版、App開發(fā)、電子商務(wù)小程序開發(fā)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)